文件放在哪里才能读取?

发布于 2024-09-06 19:30:30 字数 469 浏览 2 评论 0原文

嘿,我想使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

岁月静好 2024-09-13 19:30:30

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.

一笔一画续写前缘 2024-09-13 19:30:30

这取决于您的系统,但在大多数情况下,如果您使用相对路径打开文件,它将找到相对于进程的工作目录(即相对于您启动程序的位置)的文件。

因此,如果您只是尝试打开“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.

无敌元气妹 2024-09-13 19:30:30

我仍然会说指定路径而不是依赖当前工作目录(尽管由于某种原因它之前对您不起作用)。

尝试一下:
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.

几味少女 2024-09-13 19:30:30

如果您仍然对 Code::Blocks 感兴趣,则需要修改 Target Properties,转到 Project ->属性->构建目标并更改项目的调试/发布文件夹的执行工作目录

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文