在 C++ 中按完整路径打开文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/843083/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Open file by its full path in C++
提问by
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.
这似乎不起作用。
回答by Greg Hewgill
Normally one uses the backslash character as the path separator in Windows. So:
通常在 Windows 中使用反斜杠字符作为路径分隔符。所以:
ifstream file;
file.open("C:\Demo.txt", ios::in);
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
.
请记住,当用 C++ 源代码编写时,您必须使用双反斜杠,因为反斜杠字符本身在双引号字符串中意味着一些特殊的东西。所以上面指的是文件C:\Demo.txt
.
回答by Greg Hewgill
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.
您可以对 fstream 类使用完整路径。以下代码尝试打开 C: 驱动器根目录中的文件 demo.txt。请注意,由于这是一个输入操作,因此该文件必须已经存在。
#include <fstream>
#include <iostream>
using namespace std;
int main() {
ifstream ifs( "c:/demo.txt" ); // note no mode needed
if ( ! ifs.is_open() ) {
cout <<" Failed to open" << endl;
}
else {
cout <<"Opened OK" << endl;
}
}
What does this code produce on your system?
此代码在您的系统上生成什么?
回答by phoad
The code seems working to me. I think the same with @Iothar.
代码似乎对我有用。我认为@Iohar 也是如此。
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..
要进行交叉检查,请检查您是否可以使用 fopen 打开它。
FILE *f = fopen("C:/Demo.txt", "r");
if (f)
printf("fopen success\n");
回答by jave.web
For those who are getting the path dynamicly... e.g. drag&drop:
对于那些动态获取路径的人......例如拖放:
Some main constructions get drag&dropped file with double quotes like:
一些主要结构使用双引号拖放文件,例如:
"C:\MyPath\MyFile.txt"
Quick and nice solution is to use this function to remove chars from string:
快速而好的解决方案是使用此函数从字符串中删除字符:
void removeCharsFromString( string &str, char* charsToRemove ) {
for ( unsigned int i = 0; i < strlen(charsToRemove); ++i ) {
str.erase( remove(str.begin(), str.end(), charsToRemove[i]), str.end() );
}
}
string myAbsolutepath; //fill with your absolute path
removeCharsFromString( myAbsolutepath, "\"" );
myAbsolutepath
now contains just C:\MyPath\MyFile.txt
myAbsolutepath
现在只包含 C:\MyPath\MyFile.txt
The function needs these libraries: <iostream>
<algorithm>
<cstring>
.
The function was based on this answer.
该函数需要这些库:<iostream>
<algorithm>
<cstring>
.
该功能基于此答案。
Working Fiddle:http://ideone.com/XOROjq
工作小提琴:http : //ideone.com/XOROjq