[英]Read a file from current directory using ifstream and QtCreator
I'm using Qt Creator for a plain C++ project without the Qt libraries. 我正在使用Qt Creator进行没有Qt库的普通C ++项目。
I'm trying to open a file like this: 我正在尝试打开这样的文件:
fopen("text.txt", "r");
or 要么
ifstream fin;
fin.open("text.txt");
But it doesn't work with just the filename like in Visual Studio, I have to pass the full path for it to open the file... 但它不能像Visual Studio中那样只使用文件名,我必须通过它的完整路径来打开文件...
Anybody knows why is that? 谁知道为什么会这样? and how can I refer to the current directory without using Qt libs?
如何在不使用Qt库的情况下引用当前目录?
You can use QDir::current()
to check wether the working directory is what you want it to be. 您可以使用
QDir::current()
来检查工作目录是否是您想要的。 Without Qt you can use the solution TomA linked to . 没有Qt,您可以使用TomA链接的解决方案 。
The run settings allow you to configure it for running the application from the IDE. 运行设置允许您配置它以从IDE运行应用程序。
It will; 它会; your code is fine.
你的代码很好。 But as other answers allude, you need to make sure you're running it in the directory you think you are.
但正如其他答案所暗示的那样,您需要确保在您认为自己的目录中运行它。
On the left panel select "Projects" then (from the tabs at the top) "Run Settings" and it will show you where it runs the executable from in the field labeled "Working directory". 在左侧面板中选择“Projects”,然后选择(从顶部的选项卡)“Run Settings”,它将在标记为“Working directory”的字段中显示它运行可执行文件的位置。 I think by default it's the directory above the release and debug folders.
我认为默认情况下它是发布和调试文件夹上方的目录。
The difference between Visual Studio and Qt Creator may be that Visual Studio和Qt Creator之间的区别可能就是这样
Try to get the current directory using this and then see if it actually contains the file. 尝试使用此目录获取当前目录,然后查看它是否实际包含该文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.