【发布时间】:2014-02-16 23:04:10
【问题描述】:
不幸的是,我只是一个程序员的新手才能弄清楚。我想将以下内容作为while( getline(getline(fin, line) ) 行,因为我试图从文件中读取整行文本。然后我试图弄清楚该文件中是否有任何相似的单词或数字。我在 Microsoft Visual Studio 2012 中写这篇文章。
#include <iostream>
#include <fstream>
#include <sstream>
#include <cctype>
#include <string>
using namespace std;
// main application entry point
int main(int argc, char * argv[])
{
string filename;
ifstream inFile;
// request the file name from the user
cout << "Please enter a filename: ";
// stores the users response in the string called filename
cin >> (std::cin, filename);
// opens the file
inFile.open(filename.c_str());
// if the file doesn't open
if (!inFile)
{
cout << "Unable to open file: " << filename << endl;
return -1;
} // end of if( !inFile )
// while( getline(getline(fin, line) ) gives me the same error
while (getline())
{}
// close the file
inFile.close();
} // end of int main( int argc, char* argv[])
【问题讨论】:
-
只是好奇:你认为
cin >> (std::cin, filename)会做什么? -
我认为是这样的://将用户响应存储在名为 filename 的字符串中,但在您发表评论后,看来我完全错了。
-
不,它这样做是对的,只是你以不必要的方式这样做。
(std::cin, filename)与filename相同,因为逗号运算符,返回最右边的操作数。你真正需要的是std::cin >> filename。 -
完整的错误信息是什么?
标签: c++ compiler-errors getline file-io