10 Replies - 639 Views - Last Post: 02 September 2013 - 09:10 PM Rate Topic: -----

#1 chaostheory12  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 8
  • Joined: 15-November 12

Reading data from a text file into an array of structures

Posted 01 September 2013 - 11:25 PM

Hi. I'm trying to get information from a text file "cars_inventory.txt" into an array of CAR structures. The text file that I'm reading from looks like this
2000 Buick LeSabre
2 door
white
46,400
Y
2000BUICLE00397
15950.00
John Doe

and the structure I'm sending it to looks like this
typedef struct car
{
  int year;
  char make[25];
  char model[25];
  char style[25];
  char color[20]; 
  char mileage[8];
  char air_condition;
  char inventory_num[16];
  double price;
  char previous_owner[30];      
}CAR;

I have a function set up to read the data from the text file using fscanf through a loop to the end of the file. The problems arise that 1. my loop doesn't terminate at the end of the file, 2. the data doesn't read into the array right. Printing it out with one element per line did this
2000
Buick
LeSabre
2
door
white


46,400
0.00000
Y


Here's the code for the function.
CAR readFile( CAR input[] ) 
{
   FILE *readData;
   char *inputName = "car_inventory.txt";
   int index = 0;

   readData = fopen( inputName, "r" );

   if( readData == NULL ) 
   {
      printf( "File cannot be opened\n" );
   } /* end if */
   else {
      while( !feof( readData ) ) 
      {
          fscanf(readData,"%d", &input[index].year);
          fscanf(readData,"%s", &input[index].make);
          fscanf(readData,"%s", &input[index].model);
          fscanf(readData,"%s", &input[index].style);
          fscanf(readData,"%s", &input[index].color);
          fscanf(readData,"%s", &input[index].mileage);
          fscanf(readData,"%c", &input[index].air_condition);
          fscanf(readData,"%s", &input[index].inventory_num);
          fscanf(readData,"%lf", &input[index].price);
          fscanf(readData,"%s", &input[index].previous_owner);

          printf("%d", input[index].year);
          printf("%s", input[index].make);
          printf("%s", input[index].model);
          printf("%s", input[index].style);
          printf("%s", input[index].color);
          printf("%s", input[index].mileage);
          printf("%c", input[index].air_condition);
          printf("%s", input[index].inventory_num);
          printf("%lf", input[index].price);
          printf("%s", input[index].previous_owner);

        index++;
     } /* end while */

     fclose( readData );
  } /* end else */

  return input;
} /* end function readFile */



Is This A Good Question/Topic? 0
  • +

Replies To: Reading data from a text file into an array of structures

#2 salazar  Icon User is offline

  • D.I.C Addict

Reputation: 87
  • View blog
  • Posts: 539
  • Joined: 26-June 13

Re: Reading data from a text file into an array of structures

Posted 02 September 2013 - 12:13 AM

I think it may that the input for your style data is mad up of two parts a number (2) and a cstring (door). That explains both problems. It is expecting to read more data and never reached eof, and it also reads in the data incorrectly at the same time.

Try having the style data formatted as 2-door. Its the fastest way to deal with it.
Was This Post Helpful? 0
  • +
  • -

#3 Java Student  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 21
  • View blog
  • Posts: 480
  • Joined: 05-February 10

Re: Reading data from a text file into an array of structures

Posted 02 September 2013 - 07:07 AM

Another way you could do it, perhaps giving less format errors, but you would have to make each item have its own line(ie: line1="2", line2="door"):

while(!readdata.eof()){
   std::getline (readdata, number_of_doors);
   //...keep reading
}


Was This Post Helpful? 0
  • +
  • -

#4 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 656
  • View blog
  • Posts: 2,247
  • Joined: 31-December 10

Re: Reading data from a text file into an array of structures

Posted 02 September 2013 - 07:23 AM

The problem is when does EOF get set? You need to attempt to get input before EOF is set. So it's not a good idea to have multiple fscanf() calls without checking for EOF. The scanf() functions also return the number of items input as an integer, so you could check the return values as well.
Was This Post Helpful? 1
  • +
  • -

#5 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: Reading data from a text file into an array of structures

Posted 02 September 2013 - 08:01 AM

feof in C, doesn't work the way you probably expect it to work. Generally causing the last line in the file to be printed out twice.

Instead of:
while(!feof(readData)) {
   //several fscanf()'s

   //followed by several printf()'s
}

Try this:
while((fscanf(readData,"%d", &input[index].year))>0) {
   //if you read another year, fscanf() will return 1, so you know you should
   //have a full car's record to be read and printed.

   //so get the rest of the struct members for this record, 
   //and print them as usual.
}



If you reach EOF or bad year data that can't be read by fscanf(), then the loop will end. This is not full error elimination by any means, but it's a start, and it will read the file data without repeating the last line.
Was This Post Helpful? 1
  • +
  • -

#6 chaostheory12  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 8
  • Joined: 15-November 12

Re: Reading data from a text file into an array of structures

Posted 02 September 2013 - 09:13 AM

View PostAdak, on 02 September 2013 - 08:01 AM, said:

feof in C, doesn't work the way you probably expect it to work. Generally causing the last line in the file to be printed out twice.

Instead of:
while(!feof(readData)) {
   //several fscanf()'s

   //followed by several printf()'s
}

Try this:
while((fscanf(readData,"%d", &input[index].year))>0) {
   //if you read another year, fscanf() will return 1, so you know you should
   //have a full car's record to be read and printed.

   //so get the rest of the struct members for this record, 
   //and print them as usual.
}



If you reach EOF or bad year data that can't be read by fscanf(), then the loop will end. This is not full error elimination by any means, but it's a start, and it will read the file data without repeating the last line.


Okay, thank you. That helped with the looping problem. I knew feof wasn't good code to be using I just couldn't really think of an alternative that didn't crash the program.
Was This Post Helpful? 0
  • +
  • -

#7 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 656
  • View blog
  • Posts: 2,247
  • Joined: 31-December 10

Re: Reading data from a text file into an array of structures

Posted 02 September 2013 - 09:28 AM

Would you mind posting your updated code?
Was This Post Helpful? 0
  • +
  • -

#8 chaostheory12  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 8
  • Joined: 15-November 12

Re: Reading data from a text file into an array of structures

Posted 02 September 2013 - 11:36 AM

Okay. My compiler decided it didn't like something about the return code for the function so I just replaced the code into my main function. The new code puts the data in basically correct until the character at input[index].aircondition, where it doesn't seem to put in anything and starts to get off again. I've tried fgets and fgetc as well as fscanf with the same results.
           else {
          	    while((fscanf(readData,"%d", &input[index].year))>0)  
                {
                 fscanf(readData,"%s", &input[index].make);
                 fgets(input[index].model, 25, readData);
                 fgets(input[index].style, 25, readData);
                 fscanf(readData,"%s", &input[index].color);
                 fscanf(readData,"%s", &input[index].mileage);
                 fscanf(readData,"%c", &input[index].air_condition);
                 fscanf(readData,"%s", &input[index].inventory_num);
                 fscanf(readData,"%lf", &input[index].price);
                 fscanf(readData,"%s", &input[index].previous_owner);

                 printf("Year:%d\n", input[index].year);
                 printf("Make:%s\n", input[index].make);
                 printf("Model:%s\n,", input[index].model);
                 printf("Style:%s\n", input[index].style);
                 printf("Color:%s\n", input[index].color);
                 printf("Mileage:%s\n", input[index].mileage);
                 printf("Air Condition:%c\n", input[index].air_condition);
                 printf("Inventory ID:%s\n", input[index].inventory_num);
                 printf("Price:%lf\n", input[index].price);
                 printf("Previous Owner%s\n", input[index].previous_owner);
                 
                 getchar();//This is so I can see what the program is doing
                 getchar();

                 index++;
              } /* end while */
          }//end else

Was This Post Helpful? 0
  • +
  • -

#9 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 656
  • View blog
  • Posts: 2,247
  • Joined: 31-December 10

Re: Reading data from a text file into an array of structures

Posted 02 September 2013 - 11:50 AM

What happens if for some reason, after you input something, you reach EOF? Right now you just keep trying to get input even if the last attempt failed. The scanf() family of functions return values to report success of failure, so you could check those. Here's a link to a reference page for the fscanf() function. Towards the bottom it tells you what the function returns.
Was This Post Helpful? 0
  • +
  • -

#10 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1335
  • View blog
  • Posts: 4,575
  • Joined: 19-February 09

Re: Reading data from a text file into an array of structures

Posted 02 September 2013 - 01:58 PM

View Postchaostheory12, on 02 September 2013 - 07:36 PM, said:

The new code puts the data in basically correct until the character at input[index].aircondition, where it doesn't seem to put in anything and starts to get off again. I've tried fgets and fgetc as well as fscanf with the same results.


Hi, try adding a space in the format string - which should remove whitespace before the air condition character:

09       fscanf(readData, " %c", 
               &input[index].air_condition);




View Postchaostheory12, on 02 September 2013 - 07:36 PM, said:

My compiler decided it didn't like something about the return code for the function so I just replaced the code into my main function.


The parameter input is an array and you say you are returning a struct. You don't really need to return an array because the array will be updated.
Was This Post Helpful? 1
  • +
  • -

#11 chaostheory12  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 8
  • Joined: 15-November 12

Re: Reading data from a text file into an array of structures

Posted 02 September 2013 - 09:10 PM

This part of the program is done. Thanks to everyone who posted with suggestions, you were very helpful.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1