# Writing to a specific place in a file

This is a discussion on Writing to a specific place in a file within the C++ Programming forums, part of the General Programming Boards category; really unclear about writing to files and reading from them. I have got this code to work though: Code: #include ...

1. ## 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.

2. There's a flag for appending to a file. I think it's ios::ate or ios::app.

3. 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. 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?

5. 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.

6. 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?

7. 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.

8. ## 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. Darn!
Alright.append to the end i must.
thank you.

10. 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.

11. 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. 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?

13. ofstream out("..\\text.txt");

14. 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.