Thread: issues reading from a file

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    145

    issues reading from a file

    Hi All,

    I have a simple code that writes and reads from file :
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <conio.h>
    
    int main()
    {
        FILE *fptr;
        char ch[]="He is a good boy";
        char *str; 
        //char str[40]; 
        
        fptr = fopen("anoop.txt","w+");
        if(fptr!=NULL)
        {
            fputs(ch,fptr);      
            fflush(fptr);    
            ///fclose(fptr); 
        } 
        
        fgets(str,40,fptr);
        printf("Contents:%s",str);
        fclose(fptr); 
        getch();   
        return 0;
    }
    Questions;

    1. Why doesn't it ouput the above string? what is wrong with the piece of code?

    Following piece of code outputs the string correctly:
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <conio.h>
    
    int main()
    {
        FILE *fptr;
        char ch[]="anoop is a good boy";
        char *str;      ///======================= QLine 1
        //char str[40];
        
        fptr = fopen("anoop.txt","w+");
        if(fptr!=NULL)
        {
            fputs(ch,fptr);      
            fflush(fptr);     ///======================= QLine 2       
            fclose(fptr); 
        } 
        fptr = fopen("anoop.txt","r");
        if(fptr!=NULL)         
        {
            fgets(str,40,fptr);
            //puts(str);
            printf("Contents:%s",str);
            fclose(fptr); 
        }       
        getch();   
        return 0;
    }
    Questions:

    1. How come it works? shouldn't it crash with the Qline1 ?
    w.r.t Qline1: *str is not initialized shouldn't crash as it may be pointing some where?

    2. Is the line Qline2 required?


    Thanks in advance

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Neither of them have any right to work at all, since both use an UNINITIALISED POINTER called str.

    You're writing crap over some random memory location, so it's just dumb luck that it works at all.

    Use the array you commented out.

    The reason for the first one not working, is that you need to do an fseek() between writing and reading to move the file 'cursor' to the point you want to access.

    As for the fflush(), you don't need to flush anything if you're going to close the file.

    But you do NEED to use fflush() if you're trying to write to a file, and then read from it.
    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.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Qline #1: It's a pointer that hasn't been assigned a value - a bit like a rabid animal, biting anyone that tries to use it. Harmless if left utterly alone, until that all important assignment of an address.

    Qline #2: No, not required at all. Some systems are set for more of a buffering (holding) scheme, than others.

    The trick is, you've written (or will write), some data into a file. So your CP (current position) in the file, is right at the end of the data. Perfect place IF you want to write out more data to the file.

    Wrong place, if you want to READ from the start of the file. You need to move the CP from the "caboose" to the "front car" of the file.

    this does it nicely:
    rewind(YourFilePointersName);

    fclose() will also reset the CP position to the front of the file, but then you need to re-open the file again, in order to read anything from it.

  4. #4
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    >> fclose() will also reset the CP position to the front of the file, but then you need to re-open the file again, in order to read anything from it.
    Which documentation says so???

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you help me about tolower() in file
    By nctar in forum C Programming
    Replies: 7
    Last Post: 05-12-2010, 10:04 AM
  2. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  3. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  4. Reading from file into structs..
    By dankas in forum C Programming
    Replies: 14
    Last Post: 10-16-2002, 10:33 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM