Save array to a text file

closed account (DSvMDjzh)
Alrighty, so I have a program that draws with a mouse cursor, as it draws it saves the mouse positions into an array. I need to save those coordinates into a text file. I am using c++ win32 with openGL. I would also like this to happen on exit.

Thanks Josh
closed account (DSvMDjzh)
So far this is the code I have
1
2
3
4
ofstream myfile;
myfile.open ("array.txt");
myfile << Data;
myfile.close();

It saves something to the text file in my folder but this is what it is saving "004BDEA0"
Use your ofstream object << operator just like you use the << operator of cout to print the elements of your array on screen.
Last edited on
One elegant solution would be to use std::copy1 and std::ostream_iterator2:
1
2
3
4
5
6
7
8
9
#include <algorithm>
#include <iterator>
//...
{
    int data[] = { 1, 2, 3 };

    ofstream out( "data.txt" );
    copy( data, data + 3, ostream_iterator<int>( out, " " ) );
}


1 http://www.cplusplus.com/reference/algorithm/copy/
2 http://www.cplusplus.com/reference/std/iterator/ostream_iterator/
Last edited on
closed account (DSvMDjzh)
The problem was that I didn't put in the array right when getting it in my file.
I did the following and got all my numbers properly
1
2
3
4
5
6
7
for(int i=0;i<65000;i++){
			myfile << Data[i][0];
			myfile << " ";
			myfile << Data[i][1];
			myfile << "\n";
		}
	myfile.close();


Thanks Josh
Topic archived. No new replies allowed.