Writing array to a file

This is a discussion on Writing array to a file within the C++ Programming forums, part of the General Programming Boards category; I am trying to write my array to a file but I have a slight problem. The code below writes ...

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    17

    Writing array to a file

    I am trying to write my array to a file but I have a slight problem. The code below writes the array to the file but it also writes a new line after each element is written and I want the whole array to be written on 1 line. Is there a way to get what I want with ofstream?

    Code:
    void write_array(void)
    {
        ofstream outdata;
    
        outdata.open("output.txt");
    
        if( !outdata )
        { 
            cerr << "Error: file could not be opened" << endl;
            exit(1);
        }
    	for (int m=0;m<row_3_count;m++)
                outdata << code[3][m] << endl;
    			
    	for (int m=0;m<row_4_count;m++)
                 outdata << code[4][m] << endl;
    
            for (int m=0;m<string_count;m++)
                  outdata << code[1][m] << endl;
                  outdata.close();
    }

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    2,577
    I would remove the "<< endl" from inside your for loop.

    Tim S.

  3. #3
    Registered User
    Join Date
    Jan 2010
    Posts
    17
    Quote Originally Posted by stahta01 View Post
    I would remove the "<< endl" from inside your for loop.

    Tim S.
    Aah that does it, thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Writing array of objects in a file
    By Dynamis in forum C++ Programming
    Replies: 14
    Last Post: 01-16-2011, 01:21 AM
  2. Writing a text file to an array?
    By Raen in forum C Programming
    Replies: 11
    Last Post: 10-15-2010, 06:26 PM
  3. Writing array, to file
    By zootreeves in forum C Programming
    Replies: 9
    Last Post: 09-08-2007, 05:06 PM
  4. Writing an array to a file
    By HAssan in forum C Programming
    Replies: 3
    Last Post: 10-24-2005, 06:25 AM
  5. Writing an array to a file.
    By Queatrix in forum C++ Programming
    Replies: 8
    Last Post: 04-24-2005, 01:41 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21