View Single Post
  #4 (permalink)  
Old October 23rd, 2004, 05:29 AM
klep klep is offline
Authorized User
 
Join Date: Oct 2004
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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)
Reply With Quote