.CPP File and .EXE File With Inconsistant Logic - c++

I've never run into this issue before where when I build my code, the main error checking if statement runs correctly in the console.
ifstream inputfile;
inputfile.open("arrayNumbers.txt");
if(inputfile) //<----- This is working correctly in the CPP, but not in the EXE
{
Unfortunately when I go to run the executable file, that if statement kicks out to the "File not opened" else portion
else
{
cout << "File did not open" << endl;
//^^^^In the exe, the program skips the rest of the code and kicks here
}
Anyone have any clue as to why this is happening?
Compiler is Visual Studio 2010
I have run it in both debug and release mode as well

It is likely to do with where the file arrayNumbers.txt is stored, move the file into the same folder with the executable and it should work.

When you run in Visual Studio the file is running from the Solution folder, when you double click the .exe you run from the Project->Debug-> folder most of the time
You can maybe add a Post Build Event to your project to automatically copy the file to your output dir.
copy $(SolutionDir)/arrayNumbers.txt $(OutDir)

Related

.txt files location for Microsoft Visual Studios C++ [duplicate]

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.

Strange (c++) debug issues using VS2013

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

Why can't my program open a file when debugging in VS2013?

This is pretty bare-bones, meant to just get the program going so I can debug the more complex parts:
//open file
cout << "Input File Name: ";
string fileName;
cin >> fileName;
ifstream file;
file.open(fileName, ios_base::in);
if (!(file.good())){
cout << "File Open Error\n";
return 0;
}
The program compiles fine. If I execute the debug executable from \Projects\[this project]\Debug\[program].exe by just double-clicking or browsing there via cmd, it will open the file (which is stored in that same directory) and the rest of the program hums along nicely (until it gets to the buggy parts I actually want to debug, anyways).
However, if I try to 'Start Debugging' from within VS2013, the above fails; it prints the error and immediately closes. The cmd window that the program is executing in when in debugging mode of course shows the directory in the title area, and it is definitely the same directory, but I guess it's looking for the file somewhere else. I tried copying it to the volume root as well, no joy there. I am certain this worked just fine in earlier versions of VS, but maybe I'm just brain-farting here. Any ideas?
In the project properties in Visual Studio, you can set the current directory in which to start the debugged program. By default, this is set to the location of the .vcxproj file, i.e. it's not the location of the executable.
At the same time, relative paths passed to std file stream constructors are interpreted relative to the program's current directory, which is why it fails for you when debugging but not when running directly. If you instead launched the program using these cmd commands:
>cd some\random\dir
>C:\path\to\your\Projects\[this project]\Debug\[program].exe
It would fail in exactly the same way.
To modify the startup directory used by Visual Studio when debugging, go to Project > Properties > Configuration Properties > Debugging > Working Directory. Note that the setting is configuration-specific (i.e. you can have a different startup dir for each configuration). If you want to set it to the directory containing the executable, you can use the macro $(OutDir).
Perhaps preferably, you might want to move the data file into the project source directory, as it's not a build artifact.

Can't read file from a C++ program on a Mac

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'

Where does Visual Studio search for txt files when conducting file management operations?

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.