程序无法打开文件

问题描述 投票:-6回答:1

问题是,每当我传递文件路径并运行代码时,它都不会读取文件。相反,它继续给输出“无法打开文件”。

#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");
}
c++ file-handling
1个回答
0
投票

在字符串文字中,“ \”斜杠是转义字符。为了使字符串文字正常工作,您需要使用“ \”对每个“ \”进行转义。换句话说,用两个斜杠替换每个“ \”,如下所示:

© www.soinside.com 2019 - 2024. All rights reserved.