我不明白为什么这不起作用。由于某种原因我收到错误:
error C2678: binary '>>' : no operator found which takes a left-hand operand of type 'std::istream' (or there is no acceptable conversion)
如果有帮助的话,我正在 Visual Studio 2010 C++ Express 中执行此操作。不知道为什么它给我这个错误我已经使用了其他程序cin
...
My Code:
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
int main(int argc, char* argv){
string file;
if (argc > 1)
{
file = argv[1];
}
else
{
cout << "Please Enter Your Filename: ";
cin >> file;
}
}
include <string>
最重要的是,我建议您使用 getline 代替,因为 >> 会在输入的第一个单词处停止。
Example:
std::cin >> file; // User inputs C:\Users\Andrew Finnell\Documents\MyFile.txt
结果是“C:\Users\Andrew”,考虑到数据直到换行符才被消耗,并且下一个 std::string 读取将自动被消耗并填充“Finnell\Docunts\MyFile.txt”,这是非常意外的
std::getline(std::cin, file);
这将消耗所有文本,直到换行符。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)