Correcting a file?

This is a discussion on Correcting a file? within the C++ Programming forums, part of the General Programming Boards category; Hi, is there a way to go backwards while writting a file and correcting something on a previous line? e.g ...

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    20

    Correcting a file?

    Hi, is there a way to go backwards while writting a file and correcting something on a previous line?

    e.g
    change:
    1,1,0,4,1,0.5,0.75
    2,1,0,4,1,5,10

    to:
    1,1,0,4,1,0.5,0.75
    2,1,0,4,1,5,9

    Thanks in advance! =o)

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,607
    You can manipulate the "get file pointer" and "put file pointer" using fstream.seekg() and fstream.seekp(). You can also get the current location of each "file pointer" using fstream.tellg() and fstream.tellp().

    Investigate these methods using your favorite C++ reference.

    gg

  3. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    20
    Thanks Codeplug!!
    umm... how do you store the position that tellp() returns? in the reference that I looked up, it just says "pos_type", what type is that exactly?
    Also after I can get back to a certain position, is there a way to delete the stuff in front of it and put in new information?

    Thanks

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,607
    tell[g,p]() will return type streampos, which you can use as if it were a long.

    There are no methods to delete or remove bytes in the middle of a file. You can replace existing bytes with new ones. For example: "123" => "1A3". If you want to do something like "123" => "13", you have to copy the file, ommitting the part you want to "delete". This goes the same for inserting data.

    For deletion, you can avoid this by using a fixed width for each number. So for example, if the biggest number would occupy 4 spaces: "2 ,1 ,0 ,4 " => "2 ,1 ,0 ,4735". Having a fixed width also makes it easy to seek[g,p]() to the Nth number in the file.

    Or you could use a binary file format.

    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  2. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  3. Making a LIB file from a DEF file for a DLL
    By JMPACS in forum C++ Programming
    Replies: 0
    Last Post: 08-02-2003, 08:19 PM
  4. Hmm....help me take a look at this: File Encryptor
    By heljy in forum C Programming
    Replies: 3
    Last Post: 03-23-2002, 09:57 AM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 01:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21