文件放在哪里才能读取?
嘿,我想使用 fstream 读取的文本文件应该放在哪里?在本教程中,http://www.gamedev.net/reference/articles/article1127。 asp,他们说
ifstream fin("input.txt");
“input.txt”位于哪里?在我尝试通过执行“C:\Users\XXXXXXX\Documents\test.in”来定向文件路径之前。然而,这似乎不起作用,fstream 数据输入不正确。
我正在使用代码块。
提前致谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
input.txt 应位于工作目录中。通常工作目录是包含可执行文件的目录。对于 Visual Studio,可以在调试选项中设置在调试器中运行时的工作目录。
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.
这取决于您的系统,但在大多数情况下,如果您使用相对路径打开文件,它将找到相对于进程的工作目录(即相对于您启动程序的位置)的文件。
因此,如果您只是尝试打开“input.txt”,它可能会在您启动程序的目录中查找。
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.
我仍然会说指定路径而不是依赖当前工作目录(尽管由于某种原因它之前对您不起作用)。
尝试一下:
ifstream fin("C:/MyDir1/MyDir2/input.txt");
或者说:
ifstream fin("C:\MyDir1\MyDir2\input.txt");
另请确保您的程序(运行该程序的帐户)至少具有此文件的读取权限。
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.
如果您仍然对 Code::Blocks 感兴趣,则需要修改
Target Properties
,转到Project ->属性->构建目标
并更改项目的调试/发布文件夹的执行工作目录
if you have still interest in Code::Blocks you need to modify the
Target Properties
, go toProject -> Properties -> Build targets
and change theExecuting Working Dir
for the debug/release folder of your project