Letting users input a file name and saving the file to a directory?

Jan 28, 2013 at 1:48pm
I know how to make a text file and let the user input it's name and save it to the application directory. How would I let the user input the name for the text file and then save it to a directory besides the application directory.?

For example I let the user input the text file name. I would want my program to save it to c:/somefolder
Jan 28, 2013 at 2:32pm
1
2
3
4
std::string filename ("default.txt");
std::cin >> filename;
std::ofstream("C:/somefolder/"+filename);
//... 
Jan 28, 2013 at 5:59pm
That works however, when I am wanting the user to input some text into it, it doesn't save the text. It will create the text file but there wont be anything on there. I've been trying to figure this out for hours!

Here is the current code I have


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
string filename;
		string personname1;
		string personname2;
		float age;
		string gender;
		char description[3000];
		cout << "Name the profile:\nEX: Some_Body\n(No Spaces! Please use underscores)\n";
		cin >> filename;

		ofstream myfile("c:/profiledata/"+filename += ".txt");

		myfile.open ("c:/profiledata/"+filename);

		system("cls");
		cout << "Enter the persons first name\nFirst Name: ";
		cin >> personname1;
		system("cls");
		cout << "Enter the person's last name\nLast Name: ";
		cin >> personname2;
		system("cls");
		cout << "Enter the person's age\nAge: ";
		cin >> age;
		system("cls");
		cout << "Enter in the person's gender\nGender: ";
		cin >> gender;
		system("cls");
		cout << "Enter in some information: \nInformation: ";
		cin.getline(description, 3000);
		cin.getline(description, 3000);
		system("cls");
		cout << "Profile Successfully created!\n";
		myfile << "First Name: " << personname1 << endl;
		myfile << "Last Name: " << personname2 << endl;
		myfile << "Age: " << age << endl;
		myfile << "Gender: " << gender << endl;
		myfile << "Information: " << description << endl;
		system("cls");
		cout << "Profile Created!\n";
Jan 28, 2013 at 8:29pm
1. It doesn't matter if there are spaces in the filename the user inputs, you just have to use std::getline because std::cin only extracts input until the next whitespace character.
2. You should delete line 12, line 10 already opens the file correctly - you're only causing problems.
3. Are you checking the file with the program still open? Usually the file is closed automatically at the end of the program and sometimes stuff isn't written to the file until it gets closed.

Unless something you did caused an error (like line 12), then there isn't really much wrong with the code to write to the file.

Also, use the std::getline function that works with std::string, not character arrays - what if the user enters more than 3000 characters? I think you also meant to getline once and not twice.
Last edited on Jan 28, 2013 at 8:29pm
Jan 31, 2013 at 1:46pm
Thanks! Seems like deleting line 12 fixed it
Topic archived. No new replies allowed.