noob- how to output file?

Hi, I am trying to get my program to allow the user to name the output file and then output it.

1
2
3
4
5
6
7
8
9
10
        ifstream file;
	file.open("test.dat"); 


cout<<("Please name the output file");

myfile.close();




test.dat file has these numbers on it.

1,2,3,4,5,6,7,8,9,10
closed account (L6b7X9L8)
1
2
3
4
5
6
7
ofstream myFile;
string file;
cout << "Save as: ";
cin >> file;
myFile.open(file.c_str());
// Do stuff
myFile.close();


That's how I do it.

EDIT:

I took 'output' the wrong way.

1
2
3
4
5
6
7
8
9
10
11
12
13
   ifstream myFile;
	string file;
	cout << "Open file: ";
	cin >> file;
	myFile.open(file.c_str());
	if(myFile.is_open())
	{
			// Get contents from file
	}
	else
	{
		cout << "\nCould not open file: " << file << "\n\n";
	}
Last edited on
Thanks but
I have the file opened and I used the data in the file but I don't know how to then take what I wrote and output the file with the stuff I wrote.
Last edited on
closed account (L6b7X9L8)
You mean display what is in the file? or to store it in an array or something?
currently I have the "test" file being stored in an array. So I added all the numbers together and I want to output the sum of those nmbers to a new file on my pc.

I hope I am making sense
closed account (L6b7X9L8)
Yup I know what you want now.

1
2
3
4
5
6
7
ofstream myFile;
string file;
cout << "Save output file as: ";
cin >> file;
myFile.open(file.c_str());
myFile << Variable;
myFile.close();


Try if that works buddy, remember to change Variable to the thing you want to save to file ( The array added together ).
What does this mean

myFile.open(file.c_str());

also I was using ifstream for my prgram. So When I changed ifstream to ofstream, this symbol << was underlined red.
Last edited on
Topic archived. No new replies allowed.