Where to place file in order to read?

情到浓时终转凉″ 提交于 2019-12-31 00:34:42

问题


Hey, where do I place a text file that I'm trying to read using fstream? In this tutorial, http://www.gamedev.net/reference/articles/article1127.asp, they say

ifstream fin("input.txt");

where would "input.txt" be located? Before I tried directing a path to the file by doing this "C:\Users\XXXXXXX\Documents\test.in". This however does not seem to work, Incorrect data input with fstream.

I'm using CodeBlocks.

Thanks in advance.


回答1:


input.txt should be in the working directory. Usually the working directory is the directory containing the executable. In the case of Visual Studio, the working directory when run in the debugger can be set in the Debug options.




回答2:


It depends on your system, but in most cases, if you open a file with a relative path, it will find the file relative to the working directory of the process (i.e., relative to the location from which you started the program).

So, if you simply try to open "input.txt" it will likely look in the directory from which you started the program.




回答3:


I would still say specify path instead of relying on current working directory (although for some reason it didn't work for you before).

Try that: ifstream fin("C:/MyDir1/MyDir2/input.txt");

or that: ifstream fin("C:\MyDir1\MyDir2\input.txt");

Also make sure that your program (account it runs under) has at least Read permission for this file.




回答4:


if you have still interest in Code::Blocks you need to modify the Target Properties, go to Project -> Properties -> Build targets and change the Executing Working Dir for the debug/release folder of your project



来源:https://stackoverflow.com/questions/3097868/where-to-place-file-in-order-to-read

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!