问题是,每当我传递文件路径并运行代码时,它都不会读取文件。相反,它继续给输出“无法打开文件”。
#include<iostream>
#include<fstream>
#include<stdlib.h>
using namespace std;
int main() {
fstream inFile;
inFile.open("C:\Users\Muhammad Shaeel\Desktop\CC\Lexical Analyser Code\Lexical Analyser Code\program.txt.txt");
if (!inFile) {
cout << "Unable to open file";
exit(0);
}
inFile.close();
return 0;
system("pause");
}
在字符串文字中,“ \”斜杠是转义字符。为了使字符串文字正常工作,您需要使用“ \”对每个“ \”进行转义。换句话说,用两个斜杠替换每个“ \”,如下所示: