I/O opening the file twice problem

Hello guys.

I've a c++ code written as the following:
1
2
3
4
5
6
7
8
ofstream myfile;
myfile.open("example.xls", ios::out)
myfile << "Hello";
myfile << "Line 1";
.
.
lots of lines
myfile.close();

This works great.

But then when I want to open the file again to start at the beginning of the file on the second column using this code
1
2
3
myfile.open("example.xls)
myfile << "\t" << "Second Column" ;
myfile.close(); 


This removes everything the first code have written and leaves only "Second Column".
I assumed it's something to deal with flags, but I've used flags such as
ios::ate , ios::app but it doesn't give me what I wanted.
I want to start again from the beginning of the file.

Thanks for your help.
I want to start again from the beginning of the file.
Open with ate and manually rewind to the beginning.

Plesa notm that if you start writing from the beginning, you will overwrite already existing information. If you need to insert information, you need another approach.
How to manually rewind to the beginning?

And why would I overwrite already existing information when I want to write in the second column which is already empty?
http://en.cppreference.com/w/cpp/io/basic_ostream/seekp

I want to write in the second column which is already empty?
You mean you already reserved space inside your file for it?

Remember: information in files is not stored in lines, columns, etc. It is stored as sequence of bytes.
For example something like
1
2
out << "Hello" << std::endl;
out << "World";
will be stored as "Hello\nWorld". You just cannot append text to the first line trivially.
Wow, Even if it's an Excel file?
Even if it is excel file. Actually excel file is a complex entity: compressed archive with several files inside:
http://officeopenxml.com/anatomyofOOXML-xlsx.php

Usually file is loaded into memory, parsed, resulting abstract content is modifies and then it is written into file at once. File is completely rewritten each time.
Topic archived. No new replies allowed.