使用getline分隔输入,使用逗号作为分隔符
Using getline to divide input, using commas as delim
我有一个带有电影信息的文本文件,用逗号分隔。我将提供一行深入的见解:
8,The Good the Bad and the Ugly,1966,2
我需要取这一行,并用逗号将不同的部分分开,以适应这个函数的格式:
void addMovieNode(int ranking, std::string title, int releaseYear, int quantity);
文本文件的信息与函数是有序的,但我不清楚getline操作是如何操作的。
我知道我可以传入像 这样的文本文件getline("moveInfo.txt", string, ",");
但是它如何转化为输出的实际情况呢?
我阅读了cplusplus网站上的手册,但这并没有帮助澄清很多。
您可以使用string
和stringstream
:
#include <sstream>
#include <string>
#include <fstream>
ifstream infile( "moveInfo.txt" );
while (infile)
{
std::string line;
if (!std::getline( infile, line,',' )) break;
std::istringstream iss(line);
int ranking, releaseYear, quantity;
std::string title;
if (!(iss >> ranking >> title >> releaseYear >> quantity)) { break; }
}
相关文章:
- Seg Fault Issue C++ (file IO / getline)
- 当用户键入分隔符时,停止getline()输入
- 为什么在逗号分隔符上下文中将预增量的结果强制转换为void
- 如何在 c++ 中使用 ',' 作为 getline 分隔符
- 如何在 std::getline 中自定义分隔符
- getline() 上的分隔符无法正常工作
- 在 std::getline() 上使用任何字符作为分隔符
- 如何阻止 cin.getline() 导致控制台在找不到分隔符时重复获取用户输入?
- 如何在C 中使用字符串作为getline()函数中的分隔符
- 我可以在函数 getline 中使用 2 个或更多分隔符C++吗?
- 如何在 getline() 中不使用 作为分隔符
- std::getline 在最后一次出现分隔符后跳过来自 std::cin 的输入,但不跳过来自 std::istrin
- 我对如何在 c++ 中使用 getline 和 strtok 解析多个分隔符感到非常困惑
- 你能指定什么不是 std::getline 中的分隔符吗?
- 转换str字符串以用于std::getline分隔符的字符串
- 使用getline分隔输入,使用逗号作为分隔符
- getline()分隔符出现问题
- 区分getline中的分隔符和换行符
- 使用分隔符存储空字符的Getline
- Getline忽略'-用两个分隔符getline