Thread: Having some trouble with reading from files

  1. #16
    Registered User
    Join Date
    Mar 2002

    Why wont the files read in?

    I can open the files, and add to them. They are written to correctly, But when i re-run the program, the files are not read in, Any ideas why?

    extern int m; // number of members
    extern int i; // number of videos
    extern int r; // number of rentals

    int counter = 0;

    while (!feof)
    fscanf (memberRead, "%s", MemberData[counter].membernumber);
    fscanf (memberRead, "%s", MemberData[counter].membername);
    fscanf (memberRead, "%s", MemberData[counter].memberaddress);
    fscanf (memberRead, "%d", MemberData[counter].membervisits);
    m = counter;

  2. #17
    Join Date
    Aug 2001
    Groningen (NL)
    Are you reading from the files at which you have written data in the previous session?

    In that case it could be the way you open files. You are opening them as "w", which means that if the file already existed will be cleared. You need to open the file for read/write. This can be done by "r+".

    fprintf (videoWrite, "%d\n", &RentalDatacounter].userrentaldate);

    Shouldn't this be without the &?

    fprintf (videoWrite, "%d\n", RentalDatacounter].userrentaldate);

  3. #18
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    > while (!feof)
    Probably because this is always false, and as a result, nothing gets read

    It reads
    feof without any () is a pointer to a function (and not a CALL to the function - big difference). All function pointers are non-NULL. You put the ! in front of it, making it a NULL pointer, which is inevitably, FALSE.

    Perhaps you meant
    while ( !feof(memberRead) )

    But this isn't much better.
    I've explained many times before the 'buggy' nature of feof(). There's no actual bug in feof(), the problem is everyone's mis-understanding of how to use it correctly

  4. #19
    Registered User
    Join Date
    Mar 2002
    I can now write correctly to the external files. But when i read the data back in to the program, it does not seem to recognize spacing? is this something to do with the use of fscanf?

    any ideas?


  5. #20
    Registered User
    Join Date
    Mar 2002
    Ahh, ive jus read in the book that fscanf does not recognize space's. So i have to use fgets()?

    How would i change this-
    fscanf (memberRead, "%s\n", MemberData[counter1].membernumber);

    to use fgets?


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 02-02-2009, 07:27 AM
  2. reading files
    By hiya in forum C++ Programming
    Replies: 7
    Last Post: 05-21-2005, 11:40 AM
  3. Files (reading from)
    By zbap in forum C Programming
    Replies: 1
    Last Post: 04-04-2003, 10:43 AM
  4. problem reading files in C
    By angelfly in forum C Programming
    Replies: 9
    Last Post: 10-10-2001, 11:58 AM
  5. Need Advice in reading files
    By jon in forum C Programming
    Replies: 4
    Last Post: 10-07-2001, 07:27 AM