Thread: Having some trouble with reading from files

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    22

    Having some trouble with reading from files

    At the top and bottom of main() i have (respectivly)

    openFiles('r');
    readDataFromFile();
    closeFiles('r');

    and

    openFiles('w');
    writeDataToFile();
    closeFiles('w');

    and then in files.c i have my code that will take thr user input and write it to the three external files. But as soon as i run the program, it crashes before anything is printed onto the screen, so there must be a problem with the reading of the files, or the code that is accessing them. This is what i have that opens the files

    void openFiles(char option)
    {
    if(option =='r')
    {
    memberRead = fopen("MemberFile.txt","r");
    videoRead = fopen("RentalFile.txt" ,"r");
    rentalRead = fopen("VideoFile.txt","r");
    }
    }

    Is this correct?

    Any help is weell appreciated.

    BlueBob

  2. #2
    Registered User
    Join Date
    Feb 2002
    Posts
    14
    you can use one function to open and close the files:

    void Files(char option)
    {
    if(option =='r')
    {
    memberRead = fopen("MemberFile.txt","r");
    videoRead = fopen("RentalFile.txt" ,"r");
    rentalRead = fopen("VideoFile.txt","r");
    }
    else
    close(memberRead);
    close(videoRead);
    close(rentalRead);
    }

    Can you give the entire code as an attachment?

  3. #3
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    It seems quite valid to me. You could put printf's in your code to check if things go wrong.

    Code:
    openFiles('r'); 
    printf ("openFiles finished\n");
    readDataFromFile(); 
    printf ("readDataFromFile finished\n");
    closeFiles('r'); 
    printf ("closeFiles finished\n");
    Also you could catch the value of the file pointers.

    Code:
    void openFiles(char option) 
    { 
        if(option =='r') 
        { 
            memberRead = fopen("MemberFile.txt","r"); 
            if (memberRead == NULL)
                printf ("Could not open memberRead\n");
            videoRead = fopen("RentalFile.txt" ,"r"); 
            if (videoRead == NULL)
                printf ("Could not open videoRead\n");
            rentalRead = fopen("VideoFile.txt","r"); 
            if (rentalRead == NULL)
                printf ("Could not open rentalRead\n");
        } 
    }
    Perhaps if you put the original file as attachment or just post it (if it is not too large) then we can have a better overview of what's going on.

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    22
    I'v tried what you have suggested, but it doesnt seem 2 have sorted the problem.

    I think the problem is with the reading of the data from the files. The action memberRead, videoRead, and rentalRead functions.

    One question - When the program is first run, and it opens the files, and goes to read in the data, how does it know how much data there is to read in? I use a loop

    m = readIntFromFile(memberRead);

    but im unsure if this is correct.

    Any ideas?

    BlueBob

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    17

    Lightbulb

    One question - When the program is first run, and it opens the files, and goes to read in the data, how does it know how much data there is to read in? I use a loop
    does ur loop, loop till EOF is hit??
    Parinoia Means Having All The Facts!

  6. #6
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    >When the program is first run, and it opens the files, and goes
    >to read in the data, how does it know how much data there is
    >to read in? I use a loop

    Code:
    while (!feof (file_ptr))
    {
        /* read data from file */
    }

    >m = readIntFromFile(memberRead);
    >but im unsure if this is correct.

    That depends on how the function readIntFromFile works. Can you post that function? Your function should call something like fscanf, fgetc or something like that to read data from the file.

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    22
    Heres my code to open the files, and which also calls the functions to read in the data. You say i should use a while loop to read up until the eof, how would i add add this? Also, is the option == 'b' part necessary?

    The problem is that when i run the program with the external files being empty, it crashes when it get to the below function. But if the files contain data it runs fine. Any ideas?

    BlueBob


    void OpenFiles(char option)
    {
    if(option == 'b' || option =='r')
    {
    memberRead = fopen("MemberFile.txt","r");
    if (memberRead == NULL)
    printf ("Could not open memberRead.\n");

    videoRead = fopen("RentalFile.txt" ,"r");
    if (videoRead == NULL)
    printf ("Could not open videoRead.\n");

    rentalRead = fopen("VideoFile.txt","r");
    if (rentalRead == NULL)
    printf ("Could not open rentalRead.\n");
    }

    if(option =='b' || option =='w')
    {
    memberWrite = fopen("MemberFile.db","w");
    if (memberWrite == NULL)
    printf ("Could not write open memberWrite.\n");

    videoWrite = fopen("RentalFile.db" ,"w");
    if (videoWrite == NULL)
    printf ("Could not write open videoWrite.\n");

    rentalWrite = fopen("VideoFile.db","w");
    if (rentalWrite == NULL)
    printf ("Could not write open rentalWrite.\n");
    }

    }

  8. #8
    Registered User
    Join Date
    Mar 2002
    Posts
    22
    shiro, your hotmail address is being rejected. Do you have an alternative address?

    BlueBob

  9. #9
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    I don't see why

    option == 'b'

    is required. To me it seems that the variable option is used to decide whether to open the files for read or the files for write. So I would use just 2 values.

    Have you seen one of the printf's appearing on the screen? If not, the files opened correctly. In the other case something went wrong with opening a file.

    Further I would suggest to make the function of type int and let it return 1 or 0. Assuming that TRUE == 1 and FALSE ==0, this would tell the function that calls OpenFiles about the status. If the function returns 1, then all files opened correctly, in the other situation, one or more files failed to open.

    Code:
    int OpenFiles (char option)
    {
        int retval = TRUE;
        
        if (option == 'r')
        {
            memberRead = fopen ("MemberFile.txt", "r");
            if (memberRead == NULL)
            {
                printf ("Could not open memberRead\n");
                retval = FALSE;
            }
            
            videoRead = fopen ("VideoFile.txt", "r");
            if (videoRead == NULL)
            {
                printf ("Could not open videoRead\n");
                retval = FALSE;
            }
    
            rentalRead = fopen ("RentalFile.txt", "r");
            if (rentalRead == NULL)
            {
                printf ("Could not open rentalRead\n");
                retval = FALSE;
            }
        }
        else
        {
            memberWrite = fopen("MemberFile.db","w"); 
            if (memberWrite == NULL) 
            {
                printf ("Could not write open memberWrite.\n"); 
                retval = FALSE;
            }       
    
            videoWrite = fopen("RentalFile.db" ,"w"); 
            if (videoWrite == NULL) 
            {
                printf ("Could not write open videoWrite.\n"); 
                retval = FALSE;            
            }
    
            rentalWrite = fopen("VideoFile.db","w"); 
            if (rentalWrite == NULL) 
            {
                printf ("Could not write open rentalWrite.\n"); 
                retval = FALSE;            
            }
        }
        
        return retval;    
    }
    Are you sure the program crashes in this function?

  10. #10
    Unregistered
    Guest

    Lightbulb

    u are writing to different files from which u are reading, i assume buy the names they are ment to be the same??

  11. #11
    Registered User
    Join Date
    Mar 2002
    Posts
    22
    No, three different files.

    Cheers Shiro. It was a problem with my account, not yours, sorry. I sorted it.

    BlueBob

  12. #12
    Registered User
    Join Date
    Mar 2002
    Posts
    17

    Lightbulb

    3different files, but when u read they have a .txt and when u write they have a .db extension
    Parinoia Means Having All The Facts!

  13. #13
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    There are six different files, there are six file pointers:

    FILE *memberRead, *memberWrite, *videoRead, *videoWrite, *rentalRead, *rentalWrite;

  14. #14
    Registered User
    Join Date
    Mar 2002
    Posts
    22
    Ive amended my code, and now it does not crash (apart from when writing video data to the video external file.

    But, when i re-run the program, none of the data is read in. The program runs correctly, and appears to load that data in, but it does not. Any ideas?

    example of reading members in-

    int counter = 0;
    printf("\nBegin reading -");

    while (!feof )
    {
    fscanf (memberRead, "%s\n", MemberData[m].membernumber);
    fscanf (memberRead, "%s\n", MemberData[m].membername);
    fscanf (memberRead, "%s\n", MemberData[m].memberaddress);
    fscanf (memberRead, "%d\n", &MemberData[m].membervisits );
    m++;
    }
    printf("Members read from file -");



    example of writing video out -

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

    int counter = 0;
    for(counter = 0; counter < r; counter++)
    {
    fprintf (videoWrite, "%s\n", RentalData[counter].usermembernumber);
    fprintf (videoWrite, "%s\n", RentalData[counter].uservideonumber);
    fprintf (videoWrite, "%d\n", &RentalData[counter].userrentaldate);
    }
    printf("Videos written to file -");
    BlueBob

  15. #15
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Maybe you need to check your scanf for EOF:

    Code:
    while(fscanf (memberRead, "%s\n", MemberData[m].membernumber) != EOF)
    {
    	fscanf (memberRead, "%s\n", MemberData[m].membername); 
    	fscanf (memberRead, "%s\n", MemberData[m].memberaddress); 
    	fscanf (memberRead, "%d\n", &MemberData[m].membervisits ); 
    	m++; 
    }

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