How do I save a text file where the name of the file is defined by the user?

simple question, but...
Here's the function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void save_file(string fileName)
//saves the file to file fileName.txt
{
  
	fstream myfile(fileName);
	if (myfile.is_open())
	{
		myfile << "Blah\n";
		myfile.close();
	}
	else cout << "Unable to open file";


}


Basically I want a function that takes a string fileName and uses that to create a new text document. How would I do that?
The user will choose the filename, so he can save the file as hello.txt or whatever.


Thanks in advance!
Last edited on
fileName.c_str()
I don't see a question...
thanks for that, but now somehow it can't open the file. It says "unable to open file" when I try running it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void save_file(string fileName)
//saves the file to file fileName.txt
{
	string test;
	test = "woohoo.txt";
	
	fstream myfile(test.c_str());
	if (myfile.is_open())
	{
		myfile << "Blah\n";
		myfile.close();
	}
	else cout << "\nUnable to open file\n";


}


Sorry about this. ._.
Last edited on
be sure that the file is in the correct folder!
hi pika,
a small correction...

#include <string>
#include <iostream>
#include <fstream>
using namespace std;

void save_file(string fileName)
//saves the file to file fileName.txt
{
//string test;
//test.c_str()
fstream myfile(fileName.c_str()); // not fstream myfile(test.c_str());

if (myfile.is_open())
{
myfile << "Success\n";
myfile.close();
}
else cout << "Unable to open file";


}
int main(int argc, _TCHAR* argv[])
{
save_file("MyFile.txt");
return 0;
}

also, make sure MyFile.txt is available in the folder where exe is created.
you can use ios::in | ios::out | ios::app as second arg to append the file.

Cheers
int main(int argc, _TCHAR* argv[]) <--- what does this do?

I don't have the file in the folder, but that's the problem: I want to be able to create one. How would I go about doing that?

Thanks for all the help so far!
int main(int argc, _TCHAR* argv[]) <--- what does this do?

just use int main(). It won't affect what you want to do. But if you're curious you could take a look here: http://msdn.microsoft.com/en-us/library/17w5ykft.aspx

I don't have the file in the folder, but that's the problem: I want to be able to create one. How would I go about doing that?

do it like this:

fstream myfile(fileName.c_str(),ios::in|ios::out);

as sundhu suggests
um... fstream myfile(fileName.c_str(),ios::in|ios::out); also doesn't work if the file doesn't exist...
I guess you could:

supposing these are the objects we use...

1
2
3
fstream file;
string name;
bool exists;

open it once for input to check if it exists:

1
2
3
file.open(name.c_str(),ios::in);
if (file.is_open()) {exists=true; file.close();}
else exists=false;

if it doesn't exist, create it:

if (!exists) {file.open(name.c_str(),ios::out); file.close();}

now, open it for input and output:

file.open(name.c_str(),ios::in|ios::out);
That is if you care about the contents of the file, if it exists... if you don't care you can always use:

file.open(name.c_str(),ios::out);

This will create the file if it doesn't exist, and if it exists it will erase its data.
Uh... You know, you can edit your posts.
Wow! Thanks so much!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
void save_file(string fileName)
//saves the file to file fileName.txt
{
	fstream myfile;
	bool exists;
	
	//check whether the file exists
	myfile.open(fileName.c_str(),ios::in);
	if (myfile.is_open())
	{
		exists=true;
		myfile.close();
	}
	else 
	{
		exists = false;
	}

	//if not create it
	if (!exists)
	{
		myfile.open(fileName.c_str(),ios::out);
		myfile.close();
	}

	//open for reading and writing
	myfile.open(fileName.c_str(),ios::in|ios::out);

	myfile << "Number of records: " << record_size-1 << endl;
	for(int i = record_size-1; i > 0; i--)
	{
		myfile << student_records[i].name << ": " << student_records[i].score << endl;
	}
	myfile << endl;
	myfile.close();



}


But there's just one more hiccup (sorry) -- it's that when I run it for the first time and the file doesn't exist, the file WILL be created but will be entirely empty. If I run it and the file does exist, then it works fine. Can someone point out what's wrong?
But there's just one more hiccup (sorry) -- it's that when I run it for the first time and the file doesn't exist, the file WILL be created but will be entirely empty. If I run it and the file does exist, then it works fine. Can someone point out what's wrong?

My bad... When you check if the file exists and it doesn't exist some error flag of myfile is set and therefore it can't be used for input or output. Just modify your code like this and it should be fine:

1
2
3
4
5
6
7
8
9
10
11
12
        //check whether the file exists
	myfile.open(fileName.c_str(),ios::in);
	if (myfile.is_open())
	{
		exists=true;
		myfile.close();
	}
	else 
	{
		exists = false;
                myfile.clear(); //<- add this here
	}

Last edited on
Thanks! Works great now!
Topic archived. No new replies allowed.