数据我想使用C++ ifstream的一个文本文件,由于某种原因,下面的代码不工作在数据读取。该文件包含由空格分隔的两个数字。但是,这段代码不会打印任何东西。任何人都可以向我解释什么是错的?ifstream的空白在阅读时有文件
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
void readIntoAdjMat(string fname) {
ifstream in(fname.c_str());
string race, length;
in >> race >> length;
cout << race << ' ' << length << endl;
in.close();
}
int main(int argc, char *argv[]) {
readIntoAdjMat("maze1.txt");
}
你应该,如果你正确地打开该文件,并且您成功地读取输入。从问题描述中,我猜测它无法打开文件。 –
你实际上并没有检查文件是否正常打开。它有可能从未打开过? – Borgleader