Where To Add a Text File in Visual Studio 2017

I can not seem to figure out how to add a text file to my project so that the program can find and open a user specified file. I copied this code from my textbook just to try and test where and how to load a text file for opening and reading data from. I have tried adding new item and adding existing item and when i run the program it still cannot find the file even though it is loaded into my project using the above methods.

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 <iomanip>
#include <fstream>

using namespace std;

int main() {

	string input;
	fstream file;
	char ch; 


	cout << "Please enter the file name. ";
	cin >> input;

	file.open(input, ios::in);

	if (file) {

		file.get(ch);
		
		while (file) {
			cout << ch;
			file.get(ch);
		}
		file.close();
	}
	else {

		cout << "Error! " << endl;
	}


	system("pause");
	return 0;

}
I have also tried adding the txt file to the same location as my .cpp and rerun the program and still get Error displayed.
If you run a program from VS the current directory is the same where the solution file (.sln) is.
If you run the program from the command line the current directory is either in the Debug or Release folder.
Also on line 32 it's better to use perror(nullptr); because it gives a more meaningful error msg.
You need to include <cstdio> for that.
Hello maenielwolf,

I do not see VS 2017 being much different than VS 2015 that I use. When I started with VS I had a hard time with where to put a text file to use with a program. Eventually I found this path to work well when the IDE is in "Debug" mode. "C:\Users\Owner\Documents\Visual Studio 2015\Projects\Project name\Project Name". Where "Project Name" is the name of the project you are working on.

If you compile the program in "Release" mode the path will change because of where the ".exe" file is put. You can either adjust the path in the program or copy the files to "...\Projects\Project Name\Release" sub directory.

Comment out lines 15 - 18 and add this code and run the program:

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
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include <chrono>  // <--- Added for lines 18 and 23.
#include <thread>  // <--- Added for lines 18 and 23.

std::string iFileName{ "Out File Test.txt" };  // <--- Add File name here.

std::ifstream inFile;

inFile.open(iFileName);

if (inFile.is_open())
{
        //  Comment out these lines when program is working the way you want.
	std::cout << "\n File " << iFileName << " is open" << std::endl;
	std::this_thread::sleep_for(std::chrono::seconds(2));  // <--- Needs header files chrono" and "thread".
}
else
{
	std::cout << "\n File " << iFileName << " did not open" << std::endl;
	std::this_thread::sleep_for(std::chrono::seconds(3));  // <--- Needs header files chrono" and "thread"
	exit(1);
}

exit(0);


When the program has run search for "Out File Test.txt" and see where it is at. Put your input file in the same directory and all should work.

Feel free to use this code when working with files. Slight changes are needed to use for output files. Remember to remove the "exit(0); at the end of the code. It is not needed for normal use.

I have found that the ".txt" file need so be in the same sub directory as the "stdafx.h", "targerver.h" and "ReadMe.txt" files.

Hope that helps,

Andy
Thank you for all the help.

Andy I put your code in to test and see if the file was in the right location and found that it needs to go in the same Project folder as my .cpp file then I simply add existing file from that directory to the solution source files.

So I know i have the file in the right location now but the original program still doesnt read and open the file. I dont understand what the issue is as the program is directly from the text so there is no reason it shouldnt work. This is why i didnt think adding the text file to the same location as my .ccp file was working. Am I missing something? Why wont my text file open and display an screen?
Last edited on
Also on line 32 it's better to use perror(nullptr); because it gives a more meaningful error msg. You need to include <cstdio> for that.

Didn't you try?
Thomas1965,

Yes I did it says "no such file or directory". I am not sure why Andy's suggestion would be able find the file though. I have the file saved in the the same location as the .cpp file. I tried adding it to the same location as the .sln file and had no luck.
Hello maenielwolf,

Sorry for the wrong code. I meant to set up the code for output to create a file, but started working with the wrong code.

One possibility, my bit of code put the file name in the variable "iFileName" where as you code takes input from the user. What might be happening there is that the file name entered might be misspelled or the extension is missing.

I tried your code and entered a file name with the extension and it worked.

Also you do not need to add the input or output files to the project solution to use them.

I do not know what you are using as a file name, but on line 16 it would work better to use "std::getline(std::cin, input);" instead of the "std::cin >> input;". So if your file name has a space in it the "std::cin" will read up to the first space and stop leaving the rest of the file name in the input buffer for any next read.

Hope that helps,

Andy
I was able to get it to work. Like the noob I am i wasnt typing in txt after the file name. Thanks again for all the help.
Topic archived. No new replies allowed.