I need to use C++ to read in text with spaces, followed by a numeric value.
我需要使用C ++来读取带有空格的文本,然后是数值。
For example, data that looks like:
例如,数据看起来像:
text1
1.0
text two
2.1
text2 again
3.1
can't be read in with 2 "infile >>"
statements. I'm not having any luck with getline
either. I ultimately want to populate a struct
with these 2 data elements. Any ideas?
不能用2“infile >>”语句读入。 getline也没有任何运气。我最终想用这两个数据元素填充结构。有任何想法吗?
4 个解决方案
#1
1
The standard IO library isn't going to do this for you alone, you need some sort of simple parsing of the data to determine where the text ends and the numeric value begins. If you can make some simplifying assumptions (like saying there is exactly one text/number pair per line, and minimal error recovery) it wouldn't be too bad to getline() the whole thing into a string and then scan it by hand. Otherwise, you're probably better off using a regular expression or parsing library to handle this, rather than reinventing the wheel.
标准IO库不会单独为您执行此操作,您需要对数据进行某种简单的解析以确定文本的结束位置和数值开始。如果你可以做一些简化的假设(比如说每行只有一个文本/数字对,并且最小的错误恢复),那么将整个事物getline()放入字符串然后手动扫描就不会太糟糕。否则,你可能最好使用正则表达式或解析库来处理这个问题,而不是重新发明*。
#2
1
Why? You can use getline providing a space as line separator. Then stitch extracted parts if next is a number.
为什么?您可以使用getline提供空格作为行分隔符。然后缝合提取的部分,如果下一个是数字。
#3
0
If you can be sure that your input is well-formed, you can try something like this sample:
如果您可以确定您的输入格式正确,您可以尝试这样的示例:
#include <iostream>
#include <sstream>
int main()
{
std::istringstream iss("text1 1.0 text two 2.1 text2 again 3.1");
for ( ;; )
{
double x;
if ( iss >> x )
{
std::cout << x << std::endl;
}
else
{
iss.clear();
std::string junk;
if ( !(iss >> junk) )
break;
}
}
}
If you do have to validate input (instead of just trying to parse anything looking like a double from it), you'll have to write some kind of parser, which is not hard but boring.
如果你必须验证输入(而不是只是尝试解析看起来像是双精度的任何东西),你将不得不编写某种解析器,这并不难,但很无聊。
#4
0
Pseudocode.
This should work. It assumes you have text/numbers in pairs, however. You'll have to do some jimmying to get all the typing happy, also.
这应该工作。但是,它假设您有成对的文本/数字。你也必须做一些噱头才能让所有的打字都快乐。
while( ! eof)
getline(textbuffer)
getline(numberbuffer)
stringlist = tokenize(textbuffer)
number = atof(numberbuffer)
#1
1
The standard IO library isn't going to do this for you alone, you need some sort of simple parsing of the data to determine where the text ends and the numeric value begins. If you can make some simplifying assumptions (like saying there is exactly one text/number pair per line, and minimal error recovery) it wouldn't be too bad to getline() the whole thing into a string and then scan it by hand. Otherwise, you're probably better off using a regular expression or parsing library to handle this, rather than reinventing the wheel.
标准IO库不会单独为您执行此操作,您需要对数据进行某种简单的解析以确定文本的结束位置和数值开始。如果你可以做一些简化的假设(比如说每行只有一个文本/数字对,并且最小的错误恢复),那么将整个事物getline()放入字符串然后手动扫描就不会太糟糕。否则,你可能最好使用正则表达式或解析库来处理这个问题,而不是重新发明*。
#2
1
Why? You can use getline providing a space as line separator. Then stitch extracted parts if next is a number.
为什么?您可以使用getline提供空格作为行分隔符。然后缝合提取的部分,如果下一个是数字。
#3
0
If you can be sure that your input is well-formed, you can try something like this sample:
如果您可以确定您的输入格式正确,您可以尝试这样的示例:
#include <iostream>
#include <sstream>
int main()
{
std::istringstream iss("text1 1.0 text two 2.1 text2 again 3.1");
for ( ;; )
{
double x;
if ( iss >> x )
{
std::cout << x << std::endl;
}
else
{
iss.clear();
std::string junk;
if ( !(iss >> junk) )
break;
}
}
}
If you do have to validate input (instead of just trying to parse anything looking like a double from it), you'll have to write some kind of parser, which is not hard but boring.
如果你必须验证输入(而不是只是尝试解析看起来像是双精度的任何东西),你将不得不编写某种解析器,这并不难,但很无聊。
#4
0
Pseudocode.
This should work. It assumes you have text/numbers in pairs, however. You'll have to do some jimmying to get all the typing happy, also.
这应该工作。但是,它假设您有成对的文本/数字。你也必须做一些噱头才能让所有的打字都快乐。
while( ! eof)
getline(textbuffer)
getline(numberbuffer)
stringlist = tokenize(textbuffer)
number = atof(numberbuffer)