Writing to multiple lines to output file

I am trying to write a program that will take a character array and write it to the next line in the file. I figured out the first line but how do i keep writing to that file on multiple lines?

1
2
3
4
5
6
7
8
9
10
11
12
13
     
     char char_array[line.size()];
     int k = 0; 
     for( int p = line.size()-1 ; p>=0 ; p-- )
     {
          char_array[k]=line[p];
          k++;
     }
    ofstream out_file;
    out_file.open("output.txt");
    out_file << char_array << endl;
    out_file.close();
    cout<<char_array<<endl;
Last edited on
closed account (Lv0f92yv)
You want to write to the same file over and over again on multiple lines? I am not sure I understand your question.
fstreams work just like normal streams:
1
2
3
4
myfile << "this is the first line\nand this is the second one!";
//or
myfile << "third line" << endl;
myfile << "and the no 4!";


Also, you shouldn't do what you do on line 2.
Arrays have to be declared with const size. char* char_array = new char[line.size()]; would be the right way.
Last edited on
Thanks hamsterman that worked, but why did you have to declare char_array as a pointer?
why did you have to declare char_array as a pointer?
Thats just how dynamic memory works. See http://www.cplusplus.com/doc/tutorial/dynamic/
Topic archived. No new replies allowed.