noob programmer needs a hand with file IO =(

Discussion in 'C' started by tichonga, Dec 4, 2007.

  1. tichonga

    tichonga New Member

    Joined:
    Dec 4, 2007
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Been working on this for a whole day now. I need to write text at a particular point in a file.

    CODE:
    Code:
    /* fseek example */
    #include <stdio.h>
    
    int main ()
    {
    FILE * pFile;
    pFile = fopen ( "myfile.txt" , "w" );
    fputs ( "This is an apple." , pFile );
    fseek ( pFile , 9 , SEEK_SET );
    fputs ( " sam" , pFile );
    fclose ( pFile );
    return 0;
    }
    OUTPUT:

    This is an apple. sam

    SHOULD BE:

    This is a sample.


    Could it be my compiler? I just have a crappy one that I downloaded online somewhere.


    fflush(pFile)

    after my first fputs() doesn't work either. =(
     
    Last edited by a moderator: Dec 4, 2007
  2. tichonga

    tichonga New Member

    Joined:
    Dec 4, 2007
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    fixt. was my compiler
     
  3. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    If you are opening the file using "w" that means its created newly and so it does not have a position you are specifying because fseek cannot put a pointer beyond EOF.
     
    Last edited: Dec 4, 2007

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice