chaostheory12's Profile User Rating: -----

Reputation: 1 Apprentice
Group:
New Members
Active Posts:
8 (0.01 per day)
Joined:
15-November 12
Profile Views:
170
Last Active:
User is offline Nov 15 2013 02:53 PM
Currently:
Offline

Previous Fields

Dream Kudos:
0
Icon   chaostheory12 has not set their status

Posts I've Made

  1. In Topic: Reading data from a text file into an array of structures

    Posted 2 Sep 2013

    This part of the program is done. Thanks to everyone who posted with suggestions, you were very helpful.
  2. In Topic: Reading data from a text file into an array of structures

    Posted 2 Sep 2013

    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
    
  3. In Topic: Reading data from a text file into an array of structures

    Posted 2 Sep 2013

    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.

My Information

Member Title:
New D.I.C Head
Age:
Age Unknown
Birthday:
Birthday Unknown
Gender:

Contact Information

E-mail:
Private

Friends

chaostheory12 hasn't added any friends yet.

Comments

chaostheory12 has no profile comments yet. Why not say hello?