You're not far off. The first issue I see is that your car struct is using arrays for cap, doors, costp, and sellp. Those fields are just numbers in your file, so they can just be ints. Here is the modified car structure:
struct car
{
char rego[6];
char make[15];
char model[10];
char colour[15];
int cap;
int doors;
int costp;
int sellp;
};
You had the right idea for reading in the data -- you can simply use >> on an input stream. You don't need to use endl at the end either. Here's a program that reads all the data in:
int main()
{
struct car car;
ifstream fin("cars.dat");
fin >> car.rego >> car.make >> car.model >> car.colour >> car.cap >> car.doors >> car.costp >> car.sellp;
}
----
Scott J. Kleper
Author, "Professional C++"
(Wrox, 2005)
|