how to open file from user inputted directory location

Pages: 12
Hello I have posted this question before but I didn't get much help(i think the person got confused with what I was asking for),

I would like to write a program that asks the user the location of a file.
example of what the user would enter/input:
C:\Users\Documents\Visual Studio 2017\file.txt

then the program opens the file.

after the file is opened the program then asks the user where they would like to save the file.
the user would enter different location
example:C:\Users\Downloads

Is it possible to write a program that can do this? I tried googling stuff related to this topic but I have yet to find something that can really help me.

(After the text file is opened the program would convert it to a csv file, I already figured out how to convert the file)

Example of console output:
------------------------------------
Please enter the location of the file you would like to convert:
C:\Users\bkara\Downloads\file.txt

converting file....

Please enter where you would like the converted file to be saved:
C:\Users\bkara\Favorites

saving file to user inputted location...
-----------------------------------------
Last edited on
When you say the program opens the file. You mean the program opens the file for itself, or the program pops up the file on your screen?

If you want to open a file as in pop it up on your desktop, you can use this:
1
2
3
4
5
std::string path = "C:\Users\Documents\Visual Studio 2017\file.txt";
std::system(path.c_str()); //opens document

//OR
std::system("C:\Users\Documents\Visual Studio 2017\file.txt");
Last edited on
the program opens the file for itself then converts that file then saves it in another location. @hoogo
@mikeyboy, I looked at those links. There is nothing about actually entering the directory location into the console.
The first link tells you how to enter text at the console, and store it into a string.

The second link tells you how to use a string that contains a file path to open a file.

Surely - surely - you can put two and two together and figure out how to connect the two?
Last edited on
Okay so you want to use ifstream and ofstream.
To open a certain file at a set location, you can refer to your other post but I'll copy it here:

To open file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <string>
#include <fstream>

int main()
{
    std::string path = "C:\Users\Documents\Visual Studio 2017\file.txt";
    std::ofstream yourFile;

    if (yourFile.open(path.c_str()))
    {
        //do things
    }
    else
        return 1;

    return 0;
}


Now consider this: when you open an ofstream file in your program and the file doesn't exist at the location
you said it to open, the file is created.

So to save the file to another location, I would open the file at the location you wish, so it creates the file, and I would then
copy all the content of the first file to the second one. You would therefore have saved your first file to another location.

EDIT: Pseudocode would go like this:
*Ask user for location and name of the file you want to open
*Open file
*Ask user for location where to save file to
*Ask user for a file name for the saved file
*Open file at path with given name ->creates file
*Read content of first file and write it to second one.
*Close both files
*You're done

I hope this is clear enough and that this helps you better than last post.

Regards,

Hugo.
Last edited on
@mikeyboy thanks for your replys. But don't you need to use libraries such as <filesystem> and <windows.h>.
For stream operations you only need to #include <fstream>
@hoogo thanks for your replys as well.

so instead of having

 
std::string path = "C:\Users\Documents\Visual Studio 2017\file.txt";


can make string path equal to a variable(directory path that the user enters into the console)?

1
2
string link;
std::string path = "link";
Last edited on
Yes, as long as it's a string. Or you can get the input directly from user:
1
2
3
4
5
6
7
std::string path;
std::ofstream yourFile;

std::cout << "Enter path for file:";
std::cin >> path;

yourFile.open(path.c_str());


If that's what you're asking

The varibale's name is irrelevant, only the way you use it matters. You can call it path, link, whatever you want, as long as you put it in the right place and use it right
Last edited on
@hoogo Alright thanks. I can work with this. I may comeback with more questions since i'm not great at coding.
Great, feel free to ask anything in doubt!
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
#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;
	ofstream yourfile;



	if (yourfile.open(path.c_str())) {

		
	}
	else
		return 1;



	return 0;

}

im not getting the error anymore
Last edited on
yourfile.open is a void function so you can't check for a bool return value.
A better way:
1
2
3
4
5
6
7
8
9
10
11
12
  string filename;
  cout << "Enter filename: ";
  // prefer over cin >> filename since windows allows 
  // filenames with spaces
  getline(cin, filename); 

  ifstream src(filename); // src(filename.c_str()) for old compilers
  if(!src) // or (!src.is_open())
  {
    // handle error
  }
  // use src now 
@hoogo what would be the best way to save the file to the new location?

i have this prepared for the new location
1
2
3
4
5
string link2;
cout << "enter path you would like save the new file:";
cin >> link2;

string path = link2;


do i just use this command again? is there another command to save files to a user inputted location?
 
yourfile.open(path.c_str())

Last edited on
Ok this is what i have so far

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;
	ofstream yourfile;

	yourfile.open(path.c_str());

	if (!yourfile) {

		cout << "File Not Found";

		return 0;
		
	}
	else {

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

		ifstream yourfile;
		ofstream out("newfile.csv");

		while (getline(yourfile, line)) {

			stringstream ss(line);
			bool first = true;

			while (ss >> word) {

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

		}
		

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

		out.open(path2.c_str());

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



	return 0;

	
}


Everything in the else statement is what converts the txt file into a csv file. I don't get any errors but the program is not working correctly. it allows me to enter the file location, then the prompt to save to file in a new location comes. I cannot enter anything into this prompt, if i click enter or another key the CMD prompt just closes.
Last edited on
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
#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; This doesn't add anything, therefore is useless
	ofstream yourfile;

	yourfile.open(link.c_str()); //change here

	if (!yourfile) {

		cout << "File Not Found";

		return 1; //Return 0 means everything went fine, if it returns something else, then you know something went wrong
		
	}
	else {

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

		ifstream yourfile;
		ofstream out("newfile.csv"); //what are you trying to do here?

		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; useless as well

		out.open(saveLocation.c_str());

		yourfile.close();
		out.close();
	}
	
	system("PAUSE"); //Prevents the console from closing when the program has executed
	
	return 0;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
else {

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

		ifstream yourfile;
		ofstream out("newfile.csv");

		while (getline(yourfile, line)) {

			stringstream ss(line);
			bool first = true;

			while (ss >> word) {

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

		}

What sort of conversion are you trying to do? Can you show us the file you're trying to convert and what you want to convert it into?
Last edited on
@hoogo I am trying to convert a txt file to a csv file. The function in the else statement replaces the whitespaces with commas. the txt file looks like this:
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

I have already figured that part out though.
Why soo complicated ?

1
2
3
4
5
6
7
Read the source filename.
Open the input stream with filename
Open the outstream.
While reading a line from input 
  repeat ' ' with ',' in the line
  write the line to putput
End-While
Pages: 12