how to open file from user inputted directory location

Pages: 12
Jun 25, 2018 at 12:48pm
@Thomas1965 the code I had written before was much more complicated until i simplified it to this. so I don't think it is complicated. and the conversion from txt to csv works so there is no problem with that part of the code.

I am just having trouble with opening a file in a user inputted location then saving the converted file to another user inputted location.
Jun 25, 2018 at 1:47pm
ok this is what i have so far again. I made minor changes:

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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>

using namespace std;

int main() {

	string link;
	
	cout << "Enter path for file:";
	cin >> link;

	//string path = link; useless
	ifstream yourfile; //oftream -> ifstream because i have to read the file first

	yourfile.open(link.c_str());

	if (!yourfile) {

		cout << "File Not Found";

		return 1;
		
	}
	else {

		const char comma = ',';
		string line, word;

		//ifstream yourfile;
		ofstream out("newfile.csv"); //stream class that writes on files

		while (getline(yourfile, line)) {

			stringstream ss(line);
			bool first = true;

			while (ss >> word) {

				if (!first)out << comma;
				out << word;
				first = false;
			}
			out << '\n';

		}
		

		string saveLocation;
		cout << "enter path you would like to save new file:";
		cin >> saveLocation;
		
		//string path2 = link2;

		out.open(saveLocation.c_str());

		yourfile.close();
		out.close();
	}

	system("PAUSE");

	return 0;

	
}


this is the output im getting in the CMD prompt:
Enter path for file:F:\Visual Studios Projects\practice\file
enter path you would like to save new file:Press any key to continue . . .
Press any key to continue . . .
Last edited on Jun 25, 2018 at 1:52pm
Jun 25, 2018 at 2:08pm
Didn't I mention earlier that you meed to use getline to read the filenames since >> stops a the first whitespace, also getline will remove trailing '\n'. See my first post in this thread
Jun 25, 2018 at 8:43pm
ok so i've simplified the function that i am trying to learn

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
#include<iostream>
#include<string>
#include<fstream>
//#include<sstream>
using namespace std;

int main() {

	string link;

	cout << "Enter path of the file:";
	cin >> link;

	//ifstream yourfile;
	ofstream yourfile;

	yourfile.open(link.c_str());

	if (!yourfile) {

		cout << "File Not Found";

		return 1;
	}
	else {

		string saveLocation;
		cout << "Enter the path you want to save the new file:";
		cin >> saveLocation;

		yourfile.open(saveLocation.c_str());

		yourfile.close();
	}

	system("PAUSE");

	return 0;
}


this does not save the new file in a new location. I do not know what is missing or incorrect.
Last edited on Jun 25, 2018 at 8:46pm
Jun 25, 2018 at 9:06pm
What makes it not work is the order in which you do things.

You have to open your save file before you write to it, otherwise it doesn't work.

Here is your code which I modified, I tested it and it should work the way you want it to:
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>

using namespace std;

int main()
{
	const char comma = ',';

	string link;
	string fileName;
	string saveLocation;
	string line, word;
	
	ifstream sourceFile;			//file you open first
	ofstream saveFile;			//file you create and save to


	
	cout << "Enter path for file (e.g. C:\\Users\\Me\\Documents\\myfile.txt):\n";
	cin >> link;
	
	sourceFile.open(link.c_str());

	if (!sourceFile) 
	{
		cout << "\nFile Not Found";
		return 1;	
	}
	else 
	{
		cout << "\nEnter path you would like to save new file (e.g. C:\\Users\\Me\\Desktop):\n";
		cin >> saveLocation;
		cout << "\nEnter .csv file name (e.g. file.csv): ";
		cin >> fileName;
		
		saveLocation.append("\\");
		saveLocation.append(fileName);		//These two lines add "\" and fileName to the saveLocation string
							//so that you end up with a complete path with the file included in it
		
		saveFile.open(saveLocation.c_str());   //you have to open your save file before you write on it, otherwise it
						       //doesn't work

		while (getline(sourceFile, line))
		{
			stringstream ss(line);
			bool first = true;

			while (ss >> word) {

				if (!first)saveFile << comma;
				saveFile << word;
				first = false;
			}
			saveFile << '\n';
		}
		
		cout << "\n\nFile successfully saved!\n\n";

		sourceFile.close();
		saveFile.close();
	}

	system("PAUSE");

	return 0;
}
Last edited on Jun 25, 2018 at 9:08pm
Jun 27, 2018 at 1:59pm
@hoogo thanks for your help once again.

The program works but there is one issue.

when the path has whitespaces the program does not work.
(e.g. C:\Users\Me\Downloads\test files)

I tried googling the issue but came to no solution.

would you know why this is happening?
Jun 27, 2018 at 3:49pm
Yes, If you want your file name to comprise white spaces too you need to use getline instead of cin:
1
2
3
cout << "\nEnter .csv file name (e.g. file.csv): ";
    cin.ignore(50, '\n');           //ignores the '\n' input when you press enter after typing path
    getline(cin, fileName);         //gets file name until '\n' is encountered (works to get spaces) 


That should do what you want!
Last edited on Jun 27, 2018 at 3:51pm
Jun 27, 2018 at 3:54pm
what about the path of the file and the path of the new file?
1
2
cout << "Enter path of your File(e.g. C:\\Users\\Me\\Documents\\myfile.txt):\n";
cin >> link;



1
2
cout << "\nenter the path you would like to save the CSV file (e.g. C:\\Users\\Me\\Desktop):\n";
cin >> saveLocation;


I didn't mean spaces in the file name. I meant spaces in the actual path. like if a folder had a white space in its name (e.g New folder). Sorry if i confused you.
Last edited on Jun 27, 2018 at 4:07pm
Jun 27, 2018 at 4:07pm
Oh well it's all the same:
1
2
cout << "Enter path for file (e.g. C:\\Users\\Me\\Documents\\myfile.txt):\n";
    getline(cin, link);

1
2
cout << "\nEnter path you would like to save new file (e.g. C:\\Users\\Me\\Desktop):\n";
    getline(cin, saveLocation);

1
2
cout << "\nEnter .csv file name (e.g. file.csv): ";
    getline(cin, fileName);


EDIT: That way, If you use getline() for all input, you make sure that any of the strings you want the user to input can contain white spaces.
Last edited on Jun 27, 2018 at 4:19pm
Jun 27, 2018 at 5:53pm
Wow this is some great stuff. I appreciate your help @hoogo.
Jun 27, 2018 at 5:59pm
My pleasure!
Topic archived. No new replies allowed.
Pages: 12