How to access text file during debugging using eclipse CDT

I am writing this code to access a file in eclipse CDT

ifstream inFile; ofstream outFile; string next; inFile.open("input.txt"); if (inFile.fail()) { cout << "\nCould not open input.txt"; exit(1); } 

The problem is that when I try to debug an application or run the application from within eclipse, it cannot find the input.txt file. When I launch my application from the console, it works fine and opens the file. I need to debug the application, but I can not, because for some reason eclipse ide cannot find the file.

Where should I put the file?

+4
source share
5 answers

This may be a problem with relative paths. Is input.txt in the root directory of your project? Eclipse makes the top-level project directory a working directory. If you replace "input.txt" in your code with the full file name, this should also work.

+7
source

In addition to moving the original input file, you can consider changing the working directory to launch the application.

By setting Run Configurations> [RUN]> Arguments Tab> Working Directory from $ {workspace_loc: [Project Name]} to something like $ {workspace_loc: [Project Name]} / Release, which is the usual target binary directory for Eclipse.

+4
source

Write a test program that creates a file such as "Here_I_Am.txt".

The directory in which it is found will be the directory that the executable uses as the default directory.

Add paths to your file name, if necessary, relative to the file "Here_I_Am.txt". Or put your text file in the same directory as "Here_I_Am.txt"

+1
source

I found that with Eclipse, having a file in the root directory of a project should allow it to load. However, if you want to run it from the command line, the file must be in the same folder as the executable file (something like Debug / inside the project folder).

It should work to drag and drop a file into an Eclipse project. Just make sure you select the "Copy file" option, not the link to it.

+1
source

After deselecting the default button, select the folder containing your binary. The binary can be found in Run configurations > Arguments > working directory .

+1
source

All Articles