File to Structure?

T

TheShadow1

How do i go from a file containing data, like ...

MJ914
Porsche
914-6
Irish_Green
2
2700
30000
40000

TO

struct car
{
char rego[6];
char make[15];
char model[10];
char colour[15];
int cap[4];
int doors[1];
int costp[6];
int sellp[6];
};
 
T

TheShadow1

struct car
{
char rego[6];
char make[15];
char model[10];
char colour[15];
int cap[4];
int doors[1];
int costp[6];
int sellp[6];
};


void main()

ifstream fin;

for(x = 0; x++; x > 11 || x = EOF;)
ifstream fin("cars.dat");
fin >> car.rego >> car.make >> car.model >> car.colour >> car.cap >>
car.doors >> car.costp >> car.sellp >>endl;
 
O

Owen Jacobson

How do i go from a file containing data, like ...

MJ914
Porsche
914-6
Irish_Green
2 // <--
2700 // <--
30000 // <--
40000 // <--

TO

struct car
{
char rego[6];
char make[15];
char model[10];
char colour[15];

These might be better off as std::string values, as defined in the
standard said:
int cap[4]; // <--
int doors[1]; // <--
int costp[6]; // <--
int sellp[6]; // <--
};

The corresponding fields in your input data don't look like integer
*arrays* to me...
 
M

Malte Starostik

TheShadow1 said:
struct car
{
char rego[6];
char make[15];
char model[10];
char colour[15];

Those are dangerous and cannot be used for streaming as used below, use
std::string instead of char arrays.
int cap[4];

This declares an array of four integers where you presumably wanted an
integral type capable of storing values with up to four decimal digits.
int doors[1];
int costp[6];
int sellp[6];

Same for those.
};


void main()

main() always returns int (and its body needs enclosing braces like any
other function).
ifstream fin;

for(x = 0; x++; x > 11 || x = EOF;)

You never declared x nor EOF, there's one ; too much in the for
statement and the expressions' order is most probably wrong.
ifstream fin("cars.dat");
fin >> car.rego >> car.make >> car.model >> car.colour >> car.cap >>
car.doors >> car.costp >> car.sellp >>endl;

Except for missing error checking, that should work after dropping the
HTH,
Regards,
Malte
 
T

TheShadow1

So something more like ...

struct Car
{
string rego;
string make;
string model;
string colour;
int cap;
int doors;
int costp;
int sellp;
}
 
J

John Harrison

TheShadow1 said:
So something more like ...

struct Car
{
string rego;
string make;
string model;
string colour;
int cap;
int doors;
int costp;
int sellp;
}


Yes, looks a lot better.
 
T

Thomas Matthews

TheShadow1 said:
So something more like ...

struct Car
{
string rego;
string make;
string model;
string colour;
int cap;
int doors;
int costp;
int sellp;
}

/* Data file from previous article:
MJ914
Porsche
914-6
Irish_Green
2
2700
30000
40000
*/

struct Car
{
// Same as above
friend istream& operator>>(istream& input, Car& car_);
};

istream& operator>>(istream& input, Car& new_car)
{
getline(input, rego);
getline(input, make);
getline(input, model);
getline(input, colour);
input >> cap >> doors >> costp >> sellp;
return input;
}

Now to use this:
int main(void)
{
ifstream data_file("data_file.txt");
Car my_car;

// Input car from data file:
data_file >> my_car;

return EXIT_SUCCESS;
}

I suggest you also overload operator<< so you can
display the contents of a Car.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,179
Messages
2,570,956
Members
47,509
Latest member
Jack116

Latest Threads

Top