开发者

how to access a text file while debugging with 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 when I try to debug my app or run my app from inside eclipse it cannot find the input.txt file. When I run my app from the console it works fine and opens the file. I need to debug the app but can't because for some reason the 开发者_运维技巧eclipse ide cannot find the file.

Where should I put the file?


It could be an issue with relative paths. Is input.txt in the root directory of your project? Eclipse makes the top-level directory of the project the working directory. If you replace "input.txt" in your code with the fully qualified file name, that should also work.


Other than moving your source input file, you may consider changing the working directory for launching the application.

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


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

The directory where this is found will be the directory that the executable is using as the default directory.

Add paths to your filename as appropriate, relative to the "Here_I_Am.txt" file. Or put your text file in the same directory as "Here_I_Am.txt"


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

It should work to drag and drop the file into your Eclipse project. Just make sure that you select the option to Copy the file, rather than Link to it.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜