无法使基本文件输入正常工作!(流)

Can't get basic file input to work! (fstream)

本文关键字:工作 常工作 输入 文件      更新时间:2023-10-16

为什么我的程序无法打开我的.txt文档?文档位于指定位置。我知道\i不是转义序列

#include <iostream>
#include <fstream>
using namespace std;
int main(){
    fstream fin("C:\input.txt");
    if (!fin)
    {
        cerr << "Error, couldn't open txt file!" << endl;
        return 1;
    }
    return 0;
}

也可以使用此代码将存储在 C:\\text.txt 的任何文件输入到控制台窗口中。

#include<iostream>
#include<fstream>
using namespace std;
int main() {
 ifstream myReadFile;
 myReadFile.open("D:\text.txt");
 char output[100];
 if (myReadFile.is_open()) {
    while (!myReadFile.eof()) {
        myReadFile >> output;
        cout << output;
    }
}
myReadFile.close();
return 0;   
}

文档位于指定位置。

C:\input.txt?真?

我很确定你打算:

fstream fin("C:\input.txt");

\是一个转义序列,导致单个反斜杠...

最终,这条路C:input.txt