将混合数据文件读入 C++ 细绳

发布于 2024-07-08 23:49:30 字数 268 浏览 3 评论 0原文

我需要使用 C++ 读取带有空格的文本,后跟一个数值。

例如,如下所示的数据:

text1
1.0
text two 
2.1
text2 again
3.1

无法使用 2 个 "infile >>" 语句读入。 我对 getline 没有任何运气 任何一个。 我最终想用这两个数据元素填充一个struct。 有任何想法吗?

I need to use C++ to read in text with spaces, followed by a numeric value.

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

寻找我们的幸福 2024-07-15 23:49:30

标准 IO 库不会单独为您完成此操作,您需要对数据进行某种简单的解析来确定文本结束位置和数值开始位置。 如果您可以做出一些简化的假设(例如,每行只有一个文本/数字对,并且错误恢复最少),那么将整个内容放入字符串中,然后手动扫描它也不会太糟糕。 否则,您可能最好使用正则表达式或解析库来处理此问题,而不是重新发明轮子。

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.

网名女生简单气质 2024-07-15 23:49:30

为什么? 您可以使用 getline 提供空格作为行分隔符。 如果 next 是数字,则缝合提取的部分。

Why? You can use getline providing a space as line separator. Then stitch extracted parts if next is a number.

那一片橙海, 2024-07-15 23:49:30

如果您可以确定您的输入格式正确,您可以尝试类似以下示例的操作:

#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 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.

甜宝宝 2024-07-15 23:49:30

伪代码。

这应该有效。 但是,它假设您有成对的文本/数字。 您还必须进行一些调整才能让所有打字都愉快。

while( ! eof)
   getline(textbuffer)
   getline(numberbuffer)
   stringlist = tokenize(textbuffer)
   number = atof(numberbuffer)

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文