我在使用ifstream
未在编译时定义的文件名创建 a 时遇到问题。以下示例工作正常:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
string file, word;
int count = 0;
cout << "Enter filename: ";
cin >> file;
ifstream in("thisFile.cpp");
while(in >> word)
count++;
cout << "That file has " << count << " whitespace delimited words." << endl;
}
但是,如果我将行更改为,则会ifstream in("thisFile.cpp");
出现ifstream in(file);
编译错误。为什么是这样?