Thread: Editing a text file

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    2

    Editing a text file

    Hi,
    I just started learning C++ not long ago, and has been stucked at this problem for quite sometimes.

    I have this text file which keep the records of cars.

    00001 Toyota Camry
    00002 Nissan Sunny
    00003 Honda Accord
    ...
    and so on

    The problem is when i use ofstream class and try to make a modification to the line that i want to do a lazy deletion.


    00002 Nissan Sunny -> xxxxxxxxxxxxxx (to indicate that this record is not a valid record)

    00001 Toyota Camry
    xxxxxxxxxxxxxxxxxxx
    00003 Honda Accord
    ...
    and so on

    I ended up with file that has only the modification line and the rest was erased.


    xxxxxxxxxxxxxxxxxx

    I am using a fixed length record so i do know which byte to start the modification and where to stop.

    Code:
            ofstream ofile;
    	ofile.open("test.txt, ios::out);
            ofile.seekp(20);
    	ofile.write("xxxxxxxxxxxxxx", 10 :confused: );
    	ofile.close();
    this is something that similar to the code that exists in my program.

    in summary: how do i modify a part of a text file without erasing the rest of the content. Thank you in advance, I cannot proceed with the homework if i cant solve this confusion.
    Last edited by chimpanzee; 01-21-2006 at 06:51 AM.

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    ofile.open("test.txt, ios::out | ios::ate);

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Your original code opened your file for write, truncuating the file (making its size zero):
    Code:
    ofile.open("test.txt, ios::out);
    AD's code will open the file for append, which doesn't erase the 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.

  4. #4
    Registered User
    Join Date
    Jan 2006
    Posts
    2
    Thanks a million guys. Really appreciate ur help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM
  5. Outputting String arrays in windows
    By Xterria in forum Game Programming
    Replies: 11
    Last Post: 11-13-2001, 07:35 PM