Thread: Updating an existing file

  1. #1
    Registered User
    Join Date
    Aug 2002
    Posts
    5

    Updating an existing file

    Hello,

    I have a text file and I need to change a part of it, but I'm not sure if that's possible.
    Let's say the file looks like this:
    a
    b
    c
    d

    I opened the file in the 'r+' more (update mode). I want to replace the 3rd char, 'c', with '3' but I don't know of a way to do it. I can use fseek(file,3,0) to get to the 3rd char in the file and I can write '3' there but I don't see a way to delete 'c'.

    Any help would be greatly appriciated, thanks.

  2. #2
    Registered User
    Join Date
    Feb 2004
    Posts
    79
    If you write '3' then it's as good as deleted.

    If you write a space ' ' then again it's as good as deleted.

    If you write '\0' it's ...

    And don't forget the file position indicator begins at 0.
    Last edited by c99; 03-06-2004 at 07:13 AM.
    R.I.P C89

  3. #3
    Registered User
    Join Date
    Aug 2002
    Posts
    5
    Thanks

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>fseek(file,3,0)
    You can't use fseek in that manner unless you've opened the file in binary mode. The offset parameter must be either zero, or a value returned by ftell() when using text mode. This is because of the way in which end of line markers are used in different OS's.

    The best way to change the text file, is to read it in, make the changes in memory and then write it all out again. Sometimes, you might want to write to a temporary file, then rename it to the same as the original file when you're ready.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User
    Join Date
    Feb 2004
    Posts
    79
    Hammer.

    Do you have documentation that supports the postulate that
    fseek can only be used on a file opened in binary mode?

    Else could you explain the reasoning behind the postulate?

    Thanks in advance.
    R.I.P C89

  6. #6
    Registered User
    Join Date
    Aug 2002
    Posts
    5
    I opened the file and used fseek() like this:
    Code:
    f=fopen(file_m,"r+");
    int x=fseek(f,3,0);
    and it works fine

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    The doc is the C standard. Here's the extract:

    ISO/IEC 9899:1999 (E)

    3 For a binary stream, the new position, measured in characters from the beginning of the
    file, is obtained by adding offset to the position specified by whence. The specified
    position is the beginning of the file if whence is SEEK_SET, the current value of the file
    position indicator if SEEK_CUR, or end-of-file if SEEK_END. A binary stream need not
    meaningfully support fseek calls with a whence value of SEEK_END.

    4 For a text stream, either offset shall be zero, or offset shall be a value returned by
    an earlier successful call to the ftell function on a stream associated with the same file
    and whence shall be SEEK_SET.
    The reason behind it is that some systems use 2 characters to mark end-of-line (CRLF) and some use one (LF). When running in text mode, CRLF in the file gets translated to one byte in the input stream to the program (\n). Therefore, fseeking to a specific byte offset in a text stream doesn't guarantee true offsets within the external file.

    If you go against the rules, it may work for you, it may not. That's why its better to stick within the rules.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #8
    Registered User
    Join Date
    Feb 2004
    Posts
    79
    Lior.

    Never assume just because something appears to work that it
    is right or will always work under all circumstances.

    C will most likely tear you apart in the not too distant future if you
    view things that way. Respect C and it will respect you!
    R.I.P C89

  9. #9
    Registered User
    Join Date
    Feb 2004
    Posts
    79
    Hammer.

    Okay, cool. I'll ponder that for a time.
    R.I.P C89

  10. #10
    Registered User
    Join Date
    Feb 2004
    Posts
    79
    Lior.

    Am I assuming correctly that because you opened the file in text
    mode and the edit operation was successful, that you ran the
    executable on a *nix machine?

    I'll also add that the code broke on this machine.
    R.I.P C89

  11. #11
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    In linux you don't have to worry about it but it won't work in other machines. In Windows they use 2 characters return and line feed. In linux all files are opened in binary.
    Back in the dark ages BC (Before Computers), a magical device called the Teletype Modell 33 existed. This amazing machine contained a shift register made out of a motor, with a rotor, and a keyboard ROM consisting solely of levers and sprints. It caontained a keyboard, a printer, and a paper tape reader/punch. It could transmit messages over the phones using a modem at a rate of 10 characters a second.
    The Teletype had a problem. It took two-tenths of a second to move the print head from the right side to the left. Two-tenths of a second is two characters times. If a second character came while the printhead was in the middle of a return, that character was lost.
    Pratical C by Steve Oualline
    So they made the return two characters as to not miss anything. Then when newer computers came out some(windows) of them decided to keep it that way.
    Unix uses <LINE FEED>
    MS-DOS/Windows uses <LINE FEED> <RETURN>
    Apple uses <RETURN>

  12. #12
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Pratical C by Steve Oualline
    Don't read that book, it's bad for your health.
    My best code is written with the delete key.

  13. #13
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    lol

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  2. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  3. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  5. Hmm....help me take a look at this: File Encryptor
    By heljy in forum C Programming
    Replies: 3
    Last Post: 03-23-2002, 10:57 AM