Thread: file functions

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    119

    file functions

    I've been reading up on fopen and fclose and all, and I was wondering if there was a way to open a text file and modify a character a certain amount of places into the file...for example, open a text file and change the 5th character inside the file (irregardless of what that character is) to some other character that I decide on.

    Thanks,

    Ash

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    847

  3. #3
    C/C++Newbie Antigloss's Avatar
    Join Date
    May 2005
    Posts
    216
    a simple way is
    Code:
    FILE *fp;
    size_t i;
    char ch = 'a'; /* any char you like */
    
    fp = fopen("filename", "r+");
    /* test fp here */
    
    
    for ( i = 1; i != 5; ++i )
    {
        getc(fp);
    }
    fputc(ch, fp);
    Last edited by Antigloss; 01-26-2006 at 10:53 AM. Reason: miss the `+' before

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by Antigloss
    a simple way is
    Too simple? Wrong mode and you'd still need the fseek, so you might as well skip the loop.
    http://c-faq.com/stdio/fupdate.html
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    C/C++Newbie Antigloss's Avatar
    Join Date
    May 2005
    Posts
    216
    To use fseek effectively, the file should be opened in binary mode

  6. #6
    Registered User
    Join Date
    Dec 2005
    Posts
    119
    I just got off work, and am just online real quick while I can be, I'm gonna save this page and view it later and try what you guys said (after I get some sleep - I work graveyard - thanks a lot for the help

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    If you open a file in mode "*+", don't forget to call fflush() between reading and writing the same file.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM