如何解析字符串文件Txt到数组与C ++ [英] How To Parse String File Txt Into Array With C++

查看:197
本文介绍了如何解析字符串文件Txt到数组与C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想写一个C ++程序,但我不熟悉C ++。我有一个 .txt 文件,其中包含如下值:

I am trying to write a C++ program, but I am not familiar with C++. I have a .txt file, which contains values as follows:

0
0.0146484
0.0292969
0.0439453
0.0585938
0.0732422
0.0878906

我在C ++代码中所做的工作如下:

What I have done in my C++ code is as follows:

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    string line;
    ifstream myReadFile;
    myReadFile.open("Qi.txt");
    if(myReadFile.is_open())
    {
        while(myReadFile.good())
        {
            getline(myReadFile,line);
            cout << line << endl;
        }
        myReadFile.close();
    }
    return 0;
}



我想让程序的输出成一个数组, p>

I would like to make the output of the program an array, i.e.

line[0] = 0
line[1] = 0.0146484
line[2] = 0.0292969
line[3] = 0.0439453
line[4] = 0.0585938
line[5] = 0.0732422
line[6] = 0.0878906


推荐答案

假设你希望你的数据存储为浮点数(而不是字符串),你可能想这样做:

Assuming you want your data stored as floating point numbers (not strings) you probably want to do something like this:

#include <iostream>
#include <vector>
#include <iterator>
#include <fstream>

int main() { 
    std::ifstream in("Qi.txt");

    // initialize the vector from the values in the file:
    std::vector<double> lines{ std::istream_iterator<double>(in),
                               std::istream_iterator<double>() };

    // Display the values:
    for (int i=0; i<lines.size(); i++)
         std::cout << "lines[" << i << "] = " << lines[i] << '\n';
}

只是简单地注意一下风格:因此 std :: ifstream in(Qi.txt); 优于 std :: ifstream in; in.open(Qi.txt); 。同样,最好直接从istream迭代器初始化向量的向量,而不是创建一个空向量,然后在显式循环中填充它。

Just a quick note on style: I prefer to see variables fully initialized right when you create them, so std::ifstream in("Qi.txt"); is preferable to std::ifstream in; in.open("Qi.txt");. Likewise, it's preferable to initialize the vector of lines directly from istream iterators rather than create an empty vector, then fill it in an explicit loop.

最后,注意如果你你不想使用像 while(somestream.good()) while(!somestream.eof())来控制你的循环 - 这些都是断了,所以他们没有(可靠地)正确读取文件。根据所涉及的数据类型,它们经常会看到从文件中读取最后一个项目两次。通常,你想要的东西像 while(file>> value) while(std :: getline(file,somestring))。这些在读取后立即检查文件的状态,因此一旦读取失败,它们就会退出循环,避免 while(good())样式的问题。

Finally, note that if you insist on writing an explicit loop anyway, you never want to use something like while (somestream.good()) or while (!somestream.eof()) to control your loop -- these are mostly broken, so they don't (dependably) read a file correctly. Depending on the type of data involved, they'll frequently appear to read the last item from the file twice. Usually, you want something like while (file >> value) or while (std::getline(file, somestring)). These check the state of the file immediately after reading, so as soon as reading fails they fall out of the loop, avoiding the problems of the while (good()) style.

哦,作为注释:这是写期望一个编译器(至少是这样)符合C ++ 11。对于较旧的编译器,您需要更改:

Oh, as a side note: this is written expecting a compiler that (at lest sort of) conforms with C++11. For an older compiler you'd want to change this:

    // initialize the vector from the values in the file:
    std::vector<double> lines{ std::istream_iterator<double>(in),
                               std::istream_iterator<double>() };

...如下所示:

    // initialize the vector from the values in the file:
    std::vector<double> lines(( std::istream_iterator<double>(in)),
                                std::istream_iterator<double>() );

这篇关于如何解析字符串文件Txt到数组与C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆