problems reading from a text file

This is a discussion on problems reading from a text file within the C Programming forums, part of the General Programming Boards category; hi, i'm in the process of coding a piece of software to list my cd audio collection using a binary ...

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    54

    problems reading from a text file

    hi,
    i'm in the process of coding a piece of software to list my cd audio collection using a binary search tree, so far i've not ran into many difficulties. As an addition to the program i want to be able to add to the list when i get a new cd, the list is saved to a text file when i shut down the program and gets loaded up when the program starts

    heres the code thats giving me problems:

    FILE *fp;
    printf("\n\nPlease enter the name of the file to open - ");
    gets(filename);
    sprintf(pathname, "d:\\college stuff\\ads\\merit\\%s.txt",filename);
    if((fp = fopen(pathname, "r"))==NULL)
    {
    printf("Cant find file, press any key to return to the Options Menu");
    getche();
    }
    else
    {


    while(!feof(fp))
    {
    fscanf(fp,"%[^|]|%[^|]|%d|", data.artist, data.title, &data.numberTracks);
    if((p = strchr(data.artist, '\n')) != NULL)
    {
    *p = '\0';
    }
    for(temp = 0;temp<data.numberTracks;temp++)
    {
    fscanf(fp,"%[^|]|",data.SongList[temp].SongTitle);

    }
    AddToList(root, data);
    };
    }
    fclose(fp);


    the text does get saved to file, but it includes extra information, when i did a watch on the program and stepped through it, i noticed that the 'feof' doesn't stop at the end, it wants to loop that extra one time, therefore causing extra information to be included

    the text file structure is as so:

    1----2-----3---4----5-----6-----7---8
    char|char|int|char|char|char|int|char

    numbers one and two are the artist and title of the cd, number 3 is the number of tracks, 4 is the tracks whose size will depend on 3, 5,6,7,8 is the next cd

    all i want is for the loop to stop at the end of the list and not continue any further

    please help

    thanx

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The problem is, you haven't reached EOF when you test for it. EOF must be read before it can actually be true.

    if( !feof(fp) ) AddToList(node);
    else free(node);

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    54
    dont i check EOF at the beginning of the while loop though, surely all the data would have been read by then

    i'm a bit unsure at this EOF

    any chance at elaborating a bit more please

  4. #4
    Registered User
    Join Date
    Nov 2001
    Posts
    54
    thanx quzah, managed to get it sorted

    damn i'm stupid

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    feof() checks the passed file pointer to see if EOF has been reached.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading output into a text file
    By pete212 in forum C Programming
    Replies: 8
    Last Post: 04-23-2008, 06:11 PM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  3. A bunch of Linker Errors...
    By Junior89 in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2006, 02:59 PM
  4. How to use FTP?
    By maxorator in forum C++ Programming
    Replies: 8
    Last Post: 11-04-2005, 03:17 PM
  5. Problems displaying content of a text file
    By melissa in forum C++ Programming
    Replies: 5
    Last Post: 11-12-2001, 06:13 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21