Unable to load text file

Hi! I need some help with why my code is not working as it should be. I would like to read a text file, however when runnning the code I get the error message pops up: Error opening: new_sensor.txt

My code is as follows:



#include <iostream>
#include <fstream>

using namespace std;

int main()
{
// part 1: Load the recorded signals
ifstream inFile;
inFile.open("new_sensor.txt");
if(!inFile){
cerr << "Error opening: new_sensor.txt\n";
return 1;
}

int new_signals[250];
for (int i=0; i<5; i++){
inFile >> new_signals[i];
}
inFile.close();



return 0;



}
some IDE, including visual studio, do not run the program from the active path. That means the program cannot see a file that is not where the IDE is working from, which may not be where you think it is.
the short answer is to use the full path to the file.
if you don't like that, put the file in the same folder as the program and run the program from your OS instead of the IDE. It will see it then.
Thank you for the answer! How do I add the full path? Also, I am not totally sure what IDE stands for.

Thank you for helping me.
IDE = Integrated Development Environment. Visual Studio, Code::Blocks are two of the more popular (and free) ones.

If'n you are on Windows a full path could be (depending on where the file is located):

"C:\\a_directory\\another_dir\\file_name.ext".

Of course you'd use the actual names of directories and filename.

File Explorer on Win 10 makes it easy to create a "full path." Simply navigate to the location of where your file is located, click on the path edit to the left of Find edit box and a nicely formatted "full path" (without file name) is created. Simply add the file name to the path.

For example: "D:\\Programming\\Projects\\Test\\main.cpp".

(Edited to replace single \ with double \\ so C++ doesn't whinge)
Last edited on
however the full path in a string variable usually needs \\ or / (windoze now supports this ).
'\' is a special character in c++, is why. \n \b \t and so on represent characters you can't type in code, and so \ is special to represent those, and so \\ is needed to get the single slash (its the only printable escape code I believe).
Last edited on
Topic archived. No new replies allowed.