User defined text file

Hello All,

I need help to:

Ask a user for a filename

Create a text document with the user defined filename

Basically

cout<<"filename"<<endl;
cin>>filename;
ofstream file;
file.open("filename".txt);
file.close();
What's the question? Where do you need help exactly?

Also note that this forum supports code formatting, which we all prefer. Most of us don't even read posts that contain unformatted code. Format code like this:

[code]
1
2
//Code here.
//The line numbering is automatic and a great help. 
[/code]
Well, basically i want a user to define a file name, but not have to type in the file extension, which i would like to automatically be a text file.

And sorry, this site is brand new, haven't explored everything ^^


1
2
3
class="centertext"> //Thanks
Well, you know cin >> filename reads input from the keyboard because you showed it. Do you know what type of variable you need 'filename' to be? Why don't you post a complete CPP file here? That will show us what you know and what you don't know.
You could do something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
std::string FileName;
std::string EndOfFileName = ".txt";



/*Do something with things like    cin >> FileName; */

std::string CompleteFile = FileName + EndOfFileName;

ofstream file;

file.open(CompleteFile);






Files aren't my forte, so take this as a test, but it should work.
webJose : I will work on a cpp and post it for you

Ben Duncan : Your idea worked!

Fixed It With:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <fstream>
using namespace std;

int main () 

{
	
    string filename,inputfilename, outputfilename;
		ofstream outputfile;
        cout << "Output filename: ";
		getline (cin, outputfilename);
		inputfilename = ".txt";
		filename = outputfilename + inputfilename;
		outputfile.open(filename.c_str(),ios::out | ios::binary);
	
	system("pause");
	return 0;
}
Topic archived. No new replies allowed.