如何通过 getline() 和 stringstream 获取我的字符串命令
how to get my string commands by getline() and stringstream
我想知道我是否使用正确的形式在一行中获取我的命令,然后通过一些 if
s 获取每个命令所需的信息。这是我的代码的一部分;实际上,我的 main
函数的第一部分:
string line;
stringstream ss;
while (!cin.eof())
{
getline(cin, line);
//i dont know if next line should be used
ss << line;
if (line.size() == 0)
continue;
ss >> command;
if (command == "put")
{
string your_file_ad, destin_ad;
ss >> your_file_ad >> destin_ad;
//baraye history ezafe shod
give_file(your_file_ad, p_online)->index_plus(command);
我尝试 运行 你的代码,在你的 if
中添加两个额外的 cout
,看看当用户输入 put a b
.[=20 时会发生什么=]
所以,这是我的代码:
string line;
stringstream ss;
while (true)
{
getline(cin, line);
//i dont know if next line should be used
ss << line;
if (line.size() == 0)
continue;
string command;
ss >> command;
if (command == "put")
{
string your_file_ad, destin_ad;
ss >> your_file_ad >> destin_ad;
cout << "input #1 is " << your_file_ad << endl;
cout << "input #2 is " << destin_ad << endl;
}
}
当我运行这段代码,然后如果我在控制台写put a b
,我会看到这个结果,这是正确的:
input #1 is a
input #2 is b
但似乎仅适用于第一个命令。之后命令无法正确处理。
所以,我又看了一遍代码,发现问题是,你在 while 之外初始化你的 stringstream
。
我不确定为什么它不起作用(可能已经达到 EOF 并且不能继续阅读了?),但是如果你在一段时间内移动 stringstream ss;
,它会正常工作:
string line;
while (true)
{
stringstream ss;
getline(cin, line);
//i dont know if next line should be used
ss << line;
if (line.size() == 0)
continue;
string command;
ss >> command;
if (command == "put")
{
string your_file_ad, destin_ad;
ss >> your_file_ad >> destin_ad;
cout << "input #1 is " << your_file_ad << endl;
cout << "input #2 is " << destin_ad << endl;
}
}
更新:阅读下面关于第一个代码问题的@LightnessRacesinOrbit 评论。
我想知道我是否使用正确的形式在一行中获取我的命令,然后通过一些 if
s 获取每个命令所需的信息。这是我的代码的一部分;实际上,我的 main
函数的第一部分:
string line;
stringstream ss;
while (!cin.eof())
{
getline(cin, line);
//i dont know if next line should be used
ss << line;
if (line.size() == 0)
continue;
ss >> command;
if (command == "put")
{
string your_file_ad, destin_ad;
ss >> your_file_ad >> destin_ad;
//baraye history ezafe shod
give_file(your_file_ad, p_online)->index_plus(command);
我尝试 运行 你的代码,在你的 if
中添加两个额外的 cout
,看看当用户输入 put a b
.[=20 时会发生什么=]
所以,这是我的代码:
string line;
stringstream ss;
while (true)
{
getline(cin, line);
//i dont know if next line should be used
ss << line;
if (line.size() == 0)
continue;
string command;
ss >> command;
if (command == "put")
{
string your_file_ad, destin_ad;
ss >> your_file_ad >> destin_ad;
cout << "input #1 is " << your_file_ad << endl;
cout << "input #2 is " << destin_ad << endl;
}
}
当我运行这段代码,然后如果我在控制台写put a b
,我会看到这个结果,这是正确的:
input #1 is a
input #2 is b
但似乎仅适用于第一个命令。之后命令无法正确处理。
所以,我又看了一遍代码,发现问题是,你在 while 之外初始化你的 stringstream
。
我不确定为什么它不起作用(可能已经达到 EOF 并且不能继续阅读了?),但是如果你在一段时间内移动 stringstream ss;
,它会正常工作:
string line;
while (true)
{
stringstream ss;
getline(cin, line);
//i dont know if next line should be used
ss << line;
if (line.size() == 0)
continue;
string command;
ss >> command;
if (command == "put")
{
string your_file_ad, destin_ad;
ss >> your_file_ad >> destin_ad;
cout << "input #1 is " << your_file_ad << endl;
cout << "input #2 is " << destin_ad << endl;
}
}
更新:阅读下面关于第一个代码问题的@LightnessRacesinOrbit 评论。