Thread: File input truncation

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    11

    File input truncation

    OK, I am creating a program that will load a text file of CDs into a prgram and add them to a structure. The text file will look like this:
    (Each block of three is 1 CD)

    Rating1
    Title1
    Artist1

    Rating2
    Title2
    Artist2
    ...

    Is there some way I can use fscanf to find the number of CDs, because right now it loads my CDs, and all the other garbage data around them. If I could find the number of Cds, I could put that in variable VAR in my program and all would be fine and dandy. Any ideas?

    Code:
    int loadFile (CD list[])
      {
        FILE *fp;
        char name[50];
        int i;
        int n;
    
        printf("Enter file name: ");
        scanf("%s",name);
        fp=fopen(name,"r");
    
            for (i = 0; i < 100; i++) {
            fscanf(fp,"%d\n",&list[i].rating);
            getLine(list[i].title,50,fp);
            getLine(list[i].artist,50,fp);
    
            }
            n=i;
        fclose(fp);
        return n;
      }

    Thanks in advance,
    Evan

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Well, you can just keep adding them to a linked list if you want. Otherwise one idea would be to iterate through the file incrementing a counter as each CD is found and then allocating enough room to hold them and re-iterating through the file filling out information.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  3. #3
    Registered User
    Join Date
    May 2004
    Posts
    11
    Well, I dont know what a linked list is.....
    and the second option seems viable I guess. Supposedly you can use fscanf to easily get the number of cds, however. Could I find the number of integers in the text then use that as the number of CDs?

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    How are you going to get those integers? You'd have to go through the whole file. Maybe you should just put the number of CD's at the top of the file. Then you could parse the first line and you would immediately know.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  5. #5
    Registered User
    Join Date
    May 2004
    Posts
    11
    Going through the whole file isnt a problem, they are going to be small. I cant really change the format of the inputted files though. Any ideas on how to get the integers?

    Evan

  6. #6
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Well you said they are formatted like:

    Rating1
    Title1
    Artist1

    So I don't know what data type Rating1 is but I would guess an integer or floating-point number. I assume the others are simple strings. If you aren't worried about erroneous formatting you could just keep reading the chunks until you reach the end of the file, incrementing some counter by one each record you read.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  7. #7
    Registered User
    Join Date
    May 2004
    Posts
    11
    Basically all I need is to figure out how many lines I need to input.
    Once I get that, I know how far down I need to go.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,664
    The problem is your for loop, which loops 100 times regardless of any errors your code may generate

    Assuming that the file is well-structured, then this should help
    Code:
        i = 0;
        while ( i < 100 && fscanf(fp,"%d\n",&list[i].rating) == 1 ) {
            getLine(list[i].title,50,fp);
            getLine(list[i].artist,50,fp);
            i++;
        }
    The loop will exit when the array is full, or when there is no more data to be read
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. can someone help me with these errors please code included
    By geekrockergal in forum C Programming
    Replies: 7
    Last Post: 02-10-2009, 02:20 PM
  3. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  4. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM