Thread: Writing to a specific place in a file

  1. #1
    Set Apart -- jrahhali's Avatar
    Join Date
    Nov 2002
    Posts
    256

    Writing to a specific place in a file

    really unclear about writing to files and reading from them. I have got this code to work though:
    Code:
    #include <iostream.h>
    #include <fstream.h>
    
    ofstream outFile; // object for writing to a file
    
    int main()
    {
    	ofstream outFile;
    	outFile.open("text.txt", ios::out);
    	outFile << "Hello";
    	outFile.close();
    	return 0;
    }
    There are two things i want to do.
    1)If there is text already in the file, write to the end of the file,
    2) and find a specific point in the text, and append there.

    Hopfully this is not difficult.
    Clear the mines from our Shazbot!
    Get the enemy Shazbot!

  2. #2
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    There's a flag for appending to a file. I think it's ios::ate or ios::app.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  3. #3
    Registered User
    Join Date
    May 2005
    Posts
    22
    Here's the whole list of modes:
    Code:
    ios::app   The function performs a seek to the end of file. When new bytes are written to the file, they are always appended to the end, even if the position is moved with the ostream::seekp function.
    ios::ate   The function performs a seek to the end of file. When the first new byte is written to the file, it is appended to the end, but when subsequent bytes are written, they are written to the current position.
    ios::in   The file is opened for input. The original file (if it exists) will not be truncated.
    ios::out   The file is opened for output.
    ios::trunc   If the file already exists, its contents are discarded. This mode is implied if ios::out is specified, and ios::ate, ios::app, and ios:in are not specified.
    ios::nocreate   If the file does not already exist, the function fails.
    ios::noreplace   If the file already exists, the function fails.
    ios::binary   Opens the file in binary mode (the default is text mode).
    
    Note that there is no ios::in or ios::out default mode for fstream objects. You must specify both modes if your fstream object must both read and write files.

  4. #4
    Set Apart -- jrahhali's Avatar
    Join Date
    Nov 2002
    Posts
    256
    Cool! alright, i know how to append, but how do i append to a specific point? for exampole say a file had this:
    Hello
    Welcome
    Bonjour
    Bye
    Go Away
    Salut

    and i wanted to append "Ciao" after Bonjour instread of at the end? How would i go about doing that?
    Clear the mines from our Shazbot!
    Get the enemy Shazbot!

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    The quick answer is to read the entire file into a container, change what you want, then read back into a file. Under special conditions you can replace data at a given spot in file, but I don't think you can add to file arbitrarily without the reading into program/append whereever/write back to file protocol.
    You're only born perfect.

  6. #6
    Set Apart -- jrahhali's Avatar
    Join Date
    Nov 2002
    Posts
    256
    hmm...well, is there anyway i can append a certain text after a certain amount of lines then? say instead of appending "Ciao" after Bonjour, i wanted to append "Ciao" at the 4th line?
    Clear the mines from our Shazbot!
    Get the enemy Shazbot!

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    hmm...well, is there anyway i can append a certain text after a certain amount of lines then? say instead of appending "Ciao" after Bonjour, i wanted to append "Ciao" at the 4th line?
    Yes, you read every line of your file into an array, and as you are reading into the array, when a counter reaches 4, you add "Ciao" to the array. When you are done reading into the array, you close the file, then open it again for writing, and you overwrite the file with the lines in your array.

    It is very difficult to append to a specific place in a file directly. There are enough issues involved that I would consider it advanced C++. Every time I read a post about it, I get more confused.
    Last edited by 7stud; 08-04-2005 at 10:55 AM.

  8. #8
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    APPEND means "add to END".

    You can only append to the end of a file. A file is linear... like an array... So, the only place you can append is to the end.

    Really, the only things you can do with the data in a file a file are read & write. When you append you are writing to the end of a file.

    You can read & write anywhere in the file. For example, you could change one of the characters in your file to upper case by writing one character to the correct position in your file.

    I have a program (I didn't write it) that makes small changes to wave files. It takes a few seconds to open & read a 10MB file. Then, I make a change that affects a few thousand samples. When I click SAVE, it saves "instantly" because it only overwrites the changed samples.

    I'm trying to think of an analogy...
    Imagine the characters in your file are held by people sitting in a row of seats in a stadium. Each person holds one character.

    You can append Ciao without disturbing the existing letters.

    But, if you write Caio after Bonjour, you will overwrite Buy and the following space (or carriage return/linefeed).

    In order to insert Caio, you don't have to disturb Hello, Welcome, or Bonjour. But you will have to move Bye, Go Away, and Salut. And, you have to move / preserve those last 3 words before you overwrite Bye.

  9. #9
    Set Apart -- jrahhali's Avatar
    Join Date
    Nov 2002
    Posts
    256
    Darn!
    Alright.append to the end i must.
    thank you.
    Clear the mines from our Shazbot!
    Get the enemy Shazbot!

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You can only append to the end of a file. A file is linear... like an array... So, the only place you can append is to the end.
    Or you can open the file for overwrite, so all data overwrites existing data.
    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.

  11. #11
    Banned
    Join Date
    Jun 2005
    Posts
    594
    i dont know why all these people say you can insert data into the
    middle of the file you could always use this, and i think
    there is a version of this fo C File I/O, this is of coruse
    for C++ File I/O

  12. #12
    Set Apart -- jrahhali's Avatar
    Join Date
    Nov 2002
    Posts
    256
    alright. Got some text to write in between text that was already there. took me a while but ti's done. thanks.
    i have another quesiton. how would i access a file that is 1 directory above the one i'm currently in without havning to type the full path. for example, i can specify a file like this:
    ofstream("text.txt" ios:ut);
    because text.txt is in the same directory. How abotu one above?

    edit: you know like in dos, cd.. brings you one directory above. is there somethign similar i can use?
    Last edited by jrahhali; 08-05-2005 at 02:26 AM.
    Clear the mines from our Shazbot!
    Get the enemy Shazbot!

  13. #13
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    ofstream out("..\\text.txt");

  14. #14
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    The quick answer is to read the entire file into a container, change what you want, then read back into a file. Under special conditions you can replace data at a given spot in file, but I don't think you can add to file arbitrarily without the reading into program/append whereever/write back to file protocol.
    Often, it is a good idea to use the "safe save method". This involves writing the document to a new temporary file, copying from the original and appending data as needed. When it is completely written, the temporary file is renamed to the original file name. This should avoid total data loss if the computer or program fails while the new document is being saved. It is also less memory intensive.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File Writing Problem
    By polskash in forum C Programming
    Replies: 3
    Last Post: 02-13-2009, 10:47 AM
  2. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM