在 C++ 中按完整路径打开文件
我希望用户向我提供文件所在的完整路径,而不仅仅是文件名。 我如何以这种方式打开文件?
是不是这样的:
ifstream file;
file.open("C:/Demo.txt", ios::in);
这似乎不起作用。
I want the user to give me the full path where the file exists and not just the file name. How do I open the file this way?
Is it something like this:
ifstream file;
file.open("C:/Demo.txt", ios::in);
This doesn't seem to work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
对这个问题的不同看法,可能会对某人有所帮助:
我来到这里是因为我在 Windows 上的 Visual Studio 中进行调试,并且我对所有这些
/
与\\
感到困惑讨论(在大多数情况下这确实不重要)。对我来说,问题是:“当前目录”未设置为我在 Visual Studio 中想要的内容。 它默认为可执行文件的目录(取决于您如何设置项目)。
更改方式:右键单击解决方案 -> 属性-> 工作目录
我只是提到它,因为这个问题似乎以 Windows 为中心,这通常也意味着以 VisualStudio 为中心,这告诉我这个提示可能是相关的(:
A different take on this question, which might help someone:
I came here because I was debugging in Visual Studio on Windows, and I got confused about all this
/
vs\\
discussion (it really should not matter in most cases).For me, the problem was: the "current directory" was not set to what I wanted in Visual Studio. It defaults to the directory of the executable (depending on how you set up your project).
Change it via: Right-click the solution -> Properties -> Working Directory
I only mention it because the question seems Windows-centric, which generally also means VisualStudio-centric, which tells me this hint might be relevant (:
通常在 Windows 中使用反斜杠字符作为路径分隔符。 所以:
请记住,在用 C++ 源代码编写时,必须使用双反斜杠,因为反斜杠字符本身在双引号字符串中表示特殊的含义。 所以上面引用的是文件
C:\Demo.txt
。Normally one uses the backslash character as the path separator in Windows. So:
Keep in mind that when written in C++ source code, you must use the double backslash because the backslash character itself means something special inside double quoted strings. So the above refers to the file
C:\Demo.txt
.您可以对 fstream 类使用完整路径。 以下代码尝试打开 C: 驱动器根目录中的文件 demo.txt。 请注意,由于这是输入操作,因此该文件必须已经存在。
这段代码在您的系统上会产生什么结果?
You can use a full path with the fstream classes. The folowing code attempts to open the file demo.txt in the root of the C: drive. Note that as this is an input operation, the file must already exist.
What does this code produce on your system?
该代码似乎对我有用。 我认为@Iothar 也是如此。
检查是否包含编译所需的标头。 如果已编译,请检查是否存在这样的文件,以及所有内容、名称等是否匹配,并检查您是否有权读取该文件。
要进行交叉检查,请检查是否可以使用 fopen 打开它。
The code seems working to me. I think the same with @Iothar.
Check to see if you include the required headers, to compile. If it is compiled, check to see if there is such a file, and everything, names etc, matches, and also check to see that you have a right to read the file.
To make a cross check, check if you can open it with fopen..
对于那些动态获取路径的人...例如拖放:
一些主要结构会使用双引号拖放文件,例如:
快速而好的解决方案是使用此函数从字符串中删除字符:
myAbsolutepath
现在仅包含C:\MyPath\MyFile.txt
该函数需要这些库:
;
.该函数基于此答案。
工作小提琴: http://ideone.com/XOROjq
For those who are getting the path dynamicly... e.g. drag&drop:
Some main constructions get drag&dropped file with double quotes like:
Quick and nice solution is to use this function to remove chars from string:
myAbsolutepath
now contains justC:\MyPath\MyFile.txt
The function needs these libraries:
<iostream>
<algorithm>
<cstring>
.The function was based on this answer.
Working Fiddle: http://ideone.com/XOROjq