Hi,

I am supposed to read data from an input file with following information

16 //number of destinations//
Baltimore //origin city//
MD //origin state//
Lewes //destination state//
DE //destination state//
117.0 //miles b/w previous location and and this one//
160 //estimated driving time//
Ferry //place to visit in that location//
0 //no. of nights spent//
0.00 //cost of food//
0.00 //cost of admission and misc. fees//
Cape_May
NJ
22.2
79
Victorian_Houses
1
290.00
43.50
.....
and so on...
I told what the data represents using //..........//

and I have this structure declared

typedef struct city
{
 char originCity[NAME_SIZE];
 char originState[NAME_SIZE]; 
 char destinCity[NAME_SIZE];
 char destinState[NAME_SIZE];
 float miles; 
 int hours;
 int minutes;
 char attraction[NAME_SIZE];
 int nights;
 float food; 
 float admission;
 }CITY;

After opening the file I read in the first three lines of data with this code

fscanf(ifp, "%d", &destinNum);
fscanf(ifp, "%s", originCity);
fscanf(ifp, "%s", originState);

for the rest of the input file Im using this for loop

for (i = 0; i < MAXd; i++)
   {

      fscanf(ifp, "%s", city[i].destinCity);
      fscanf(ifp, "%s", city[i].destinState);
      fscanf(ifp, "%f", &city[i].miles);
      fscanf(ifp, "%d", &city[i].hours);
      fscanf(ifp, "%d", &city[i].minutes);
      fscanf(ifp, "%s", city[i].attraction);
      fscanf(ifp, "%d", &city[i].nights);
      fscanf(ifp, "%f", &city[i].food);
      fscanf(ifp, "%f", &lcity[i].admission);
   }

I thought this would read the line 4 - 16 using for-loop for rest of locations in input file and then I would be able to print and manipulate from it. But when I print for example using this command

printf("%s", city[2].hours);

I only get garbage. Am I reading the file in wrong way? Or something else
would appreciate some help

Also, the info I get from file is to be stored in array of CITYs where each element of the array holds info for one destination/. Array size in 20

CITY citys[20];

I call the fuction

Arrayfill(citys, ifp, max);

This is prototype for definition

void Arrayfill(CITY city[], FILE* ifp, int  destinmax){
}

Your array / struct contains an origin city in every entry of the array, but you only read it once.

Consider storing this information separately.

fscanf is problematic. you can easily shoot yourself in the foot with it.

if the file is not EXACTLY as you think it is supposed to be it will write garbage. it's very easy to use scanf wrong.

consider using fgets() instead of fscanf(). then you can choose from a variety of string.h functions (strcmp, strstr, strtok) to validate what the text file contains before trying to write it into your struct.

Thanks for the help. I figured out what went wrong.

great.

now maybe for the sake of future searches, and as quid pro quo, you could briefly note what it was that you found wrong?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.