我正在阅读格式为的文件:
12, 10
15, 20
2, 10000
我想以x,y点的形式阅读这些内容。我已经开始了,但我不知道从哪里开始......这是我到目前为止所做的:
ifstream input("points.txt");
string line_data;
while (getline(input, line_data))
{
int d;
std::cout << line_data << std::endl;
stringstream line_stream(line_data);
while (line_stream >> d)
{
std::cout << d << std::endl;
}
}
如何以x,y整数形式读取每一行?
答案 0 :(得分:6)
说:
int a, b; char comma;
if (line_stream >> a >> comma >> b)
{
// process pair (a, b)
}
答案 1 :(得分:1)
ifstream input("points.txt");
int x, y;
char comma;
while (input >> x >> comma >> y)
{
cout << x << " " << y << endl;
}
答案 2 :(得分:1)
这是怎么回事?
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
std::ifstream input("points.txt");
while (!input.eof())
{
int x, y;
char separator;
input >> x >> separator >> y;
cout << x << ", " << y << endl;
}
}