I know this is a noob question, but I've worked with Python before and when you wanted to simply access a .txt file for example, all you had to do was make sure the txt file was in the same directory. I have the following C++ code below but it's not finding the Numbers.txt file that I have saved on my desktop. All I have in the file is one line of numbers of type double. All I want to do is to find the average of all of the numbers in the file. The program runs fine, but it doesn't print the output correctly. After checking to see what is printing into output by just printing output[0], I've discovered that the file is not copying it's contents into the array. Could someone clear this little problem up for me or at least point me in the right direction to a good tutorial?
int main() {
cout << "Getting File Information..." << endl;
ifstream file;
char output[100];
//int x;
file.open("Numbers.txt", ios::in); // open file
cout << "Opened File Successfully ****************" << endl;
file >> output; // empty file contents into output
cout << output; // print out contents of file
cout << "Should have printed out results by now" << endl;
//file >> x;
file.close();
return 0;
}
Visual Studio sets the working directory to YourProjectDirectory\Debug\Bin when running in debug mode. If your text file is in YourProjectDirectory, you need to account for that difference.
The easiest way to do that is to include your text files in the project and set their build action (in the Properties window) to Content.
If you're talking about running the code within the Visual Studio debugger via F5 or Debug / Start Debugging, you can set the working directory of your program via Project / <Project name> Properties / Configuration / Debugging / Working directory.
Put your text file in a directory somewhere, and set Working directory to point to that directory.
I just had this same problem, and I didn't find any of those answers to work. Then I remembered what I learned a long time ago in OOP.
What you have to do is take that text file on your desktop, and find the project folder in your visual studio projects within your computers documents, and put the text file in that folder outside of visual studio. Then in visual studio under source files, right click-> add existing item->(your text file)
:)
btw I bumped this thread because this thread said it was a good idea, and I wanted it updated for the sake of people googling the same question.
https://meta.stackexchange.com/questions/125965/is-bumping-old-questions-allowed
Working path is project directory.
Related
I am trying to read in a basic txt file, but I think the programming is not detecting the txt file. Here's my code.
int main() {
ifstream in;
in.open("testing.txt");
if (in.fail()) cout << "fail" << endl;
return 0;
}
The program is printing out fail. I created the txt file by right clicking the project and adding a new empty file. I am completely stuck, so I would appreciate any help.
As from QT Creator's documentation, you can change the working directory where your program should be executed in the project run settings:
If your file exists in a different path than the default (which is where QT Creator builds the executable), you can set it there.
I created the txt file by right clicking the project and adding a new empty file.
That creates the .txt file at your project main path, not where the executable is build actually.
I have inherited a project to work on and the initial build was developed on linux. I dont know if this matters or not but thought I would share it.
In order to debug the project on a windows machine I first use the CMakeGUI on win7 to create a Visual Studio Solutions file to open the project using Visual Studio 2013 and then set the startup project and build the project I am interested in. Up till now everything is okay. Now comes the confusing part.
On load the program is suppose to read a file lets call it in.dat and is declared in const char * inputFileName this variable is then passed through a class which attempts to open then file to obtain data.
fstream fs;
fs.open(inputFileName.c_str(), fstream::in);
if(!fs.is_open())
{
std::cout << "Cannot open input file!" << std::endl;
exit(0);
}
This where I am stumped...the file when placed in the debug folder for some reason cannot be opened i.e fs.is_open() returns false when I try debugging the application BUT if I cd directly into the debug folder of the project, outside of VS, and run the executable it runs as expected i.e fs.is_open() now returns true.
Your debugger's working directory defaults to your project's root directory and the binary is in the \debug subdirectory, resulting, in effect, to the path to the input file being wrong.
Set the debugger's working directory to \debug.
Here is more info on that:
https://msdn.microsoft.com/en-us/library/kcw4dzyf(v=vs.120).aspx
I have put it under the Source Files folder. I have set my working directory to $(SolutionDir)$(Configuration)\ (im not sure if that is right, i saw it somewhere) could someone help me troubleshoot.
int main()
{
cout << "program running" <<endl;
pair<int, unsigned int> mypair;
ifstream myfile;
myfile.open("numbers.txt", ios::in);
if (!myfile.is_open()){
cerr << "can't open input file" << endl;
}
else
{
cout << "file opened" << endl;
}
getchar();
myfile.close();
}
...output is: can't open input file
To figure out what directory you're pointing to, try to create a new file instead of opening one:
std::ofstream("out_test.txt");
Then you can find that file searching with File Explorer into the solution dir.
There is such thing as current directory for an application. When you run an app from VS working directory would be current one.
If you specify file name with relative path (or no path at all) OS will try to find that file relative to that directory. Where executable file and especially source file(s) are located completely irrelevant. So solution could be either set working directory to where numbers.txt is located (or move nubers.txt there), or use relative path something like foobar/numbers.txt or even ../foobar/numbers.txt etc or use absolute path.
okay I managed to figure it out..with help :) I did what Paulo M said :
To figure out what directory you're pointing to, try to create a new file instead of opening one:
std::ofstream("out_test.txt");
Then you can find that file searching with File Explorer into the solution dir.
After I figured out where the file got sent I added a new file w/ some integers to that dir and made sure the working directory was the same.I'm not sure if i had to change it? (can files be sent elsewhere?).
I had tried to do this before but I was not able to move my txt file into this directory by copy paste..so gave up. But anyhow I just right clicked/new text document/ and edited the doc with some numbers. then changed my program to open this new document.
saving file directly to the directory also worked. :) :) :)
but i am curious why I couldn't just paste it there?
First of all, this is part of my code:
....
string input;
getline(cin, input);
ifstream openFile;
openFile.open(input.c_str(), ios::in);
if(openFile.is_open()){
cout << "File opened" << endl;
}
else {
cout << "Cant open the file " << endl;
}
The result always "Cant open the file". I am very, very sure that the files are exists. I have data1.txt, data2.txt ... data10.txt in the same directory (I used XCode to add new empty file, add the data inside and save it).
I do another test, I create a new directory, copy paste the cpp and data files. I run in terminal, and it works, it can read the data file. Why does xcode cant read my data files? Any idea?
You need to give the full path to the files. Xcode will run the application from the build directory which is not where the code is.
If the files are copied as part of building an OSX or iOS application you should look at the bundle structure to find the directory.
You can tell Xcode to run the executable from the directory containing the data files.
Bring up the info dialog for the target executable, and change the value for 'Set the working directory to:' to either the Project directory or a custom directory'
I know this is a noob question, but I've worked with Python before and when you wanted to simply access a .txt file for example, all you had to do was make sure the txt file was in the same directory. I have the following C++ code below but it's not finding the Numbers.txt file that I have saved on my desktop. All I have in the file is one line of numbers of type double. All I want to do is to find the average of all of the numbers in the file. The program runs fine, but it doesn't print the output correctly. After checking to see what is printing into output by just printing output[0], I've discovered that the file is not copying it's contents into the array. Could someone clear this little problem up for me or at least point me in the right direction to a good tutorial?
int main() {
cout << "Getting File Information..." << endl;
ifstream file;
char output[100];
//int x;
file.open("Numbers.txt", ios::in); // open file
cout << "Opened File Successfully ****************" << endl;
file >> output; // empty file contents into output
cout << output; // print out contents of file
cout << "Should have printed out results by now" << endl;
//file >> x;
file.close();
return 0;
}
Visual Studio sets the working directory to YourProjectDirectory\Debug\Bin when running in debug mode. If your text file is in YourProjectDirectory, you need to account for that difference.
The easiest way to do that is to include your text files in the project and set their build action (in the Properties window) to Content.
If you're talking about running the code within the Visual Studio debugger via F5 or Debug / Start Debugging, you can set the working directory of your program via Project / <Project name> Properties / Configuration / Debugging / Working directory.
Put your text file in a directory somewhere, and set Working directory to point to that directory.
I just had this same problem, and I didn't find any of those answers to work. Then I remembered what I learned a long time ago in OOP.
What you have to do is take that text file on your desktop, and find the project folder in your visual studio projects within your computers documents, and put the text file in that folder outside of visual studio. Then in visual studio under source files, right click-> add existing item->(your text file)
:)
btw I bumped this thread because this thread said it was a good idea, and I wanted it updated for the sake of people googling the same question.
https://meta.stackexchange.com/questions/125965/is-bumping-old-questions-allowed
Working path is project directory.