【问题标题】:How To Parse String File Txt Into Array With C++如何使用 C++ 将字符串文件 Txt 解析为数组
【发布时间】:2013-06-30 16:01:23
【问题描述】:

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

0
0.0146484
0.0292969
0.0439453
0.0585938
0.0732422
0.0878906

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

#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;
}

我想让程序的输出变成一个数组,即

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

【问题讨论】:

    标签: c++


    【解决方案1】:

    假设您希望将数据存储为浮点数(而不是字符串),您可能需要执行以下操作:

    #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 迭代器初始化行向量,而不是创建一个空向量,然后将其填充到显式循环中。

    最后,请注意,如果您仍然坚持编写显式循环,您永远不会想使用while (somestream.good())while (!somestream.eof()) 之类的东西来控制您的循环——这些大部分都被破坏了,所以他们没有(可靠地)正确读取文件。根据所涉及的数据类型,他们经常会出现两次从文件中读取最后一项。通常,您需要while (file &gt;&gt; value)while (std::getline(file, somestring)) 之类的东西。它们在读取后立即检查文件的状态,因此一旦读取失败,它们就会退出循环,避免while (good()) 样式的问题。

    哦,顺便说一句:这是期望编译器(至少在某种程度上)符合 C++11 的。对于较旧的编译器,您需要更改:

        // 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>() );
    

    【讨论】:

    • 感谢您提供样式说明。很有帮助。
    • 当迭代器遇到空行时,向量“lines”会存储什么?
    • 我认为应该#include &lt;fstream&gt; 编译。
    • @max:是的。已更正。
    • @max:默认构造的 istream_iterator 基本上是“文件结尾”——也就是说,只有当流一直被读取到文件的结尾。
    【解决方案2】:

    首先你需要一个向量:

    std::vector<std::string> lines; // requires #include <vector>
    

    然后您需要从getline 操作中获取一个字符串,并将其推回向量中。很简单:

    for (std::string line; std::getline(myReadFile, line);) 
    {
        lines.push_back(line);
    }
    

    对于输出操作,您只需要:

    {
        int i = 0;
    
        for (auto a : lines)
        {
            std::cout << "lines[" << i++ << "] = " << a << std::endl;
        }
    }
    

    【讨论】:

    • 呵呵呵呵。向量本质上是一条线。所以你把getline 的结果放入向量中。
    • 当我编译时,我收到错误消息“警告:自动将改变 c++0x 中的含义;请删除它”
    • @IbnuSyuhada 用-std=c++11 编译,看看会发生什么。
    • @IbnuSyuhada:他指的是 C++11 中定义的 auto 的含义。听起来您可能想要更新编译器,尽管正确的标志可能会使其正确解释 auto
    【解决方案3】:
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    int main()
    {
    string line;
    ifstream myReadFile;
    myReadFile.open("Qi.txt");
    if(myReadFile.is_open())
    {
      for(int i=0;i<7;++i)
        if(myReadFile.good())
        {
            getline(myReadFile,line);
            cout<<"line["<<i<<"] = " << line << endl;
        }
        myReadFile.close();
    }
    return 0;
    }
    

    【讨论】:

    • 不鼓励仅使用代码的答案。考虑添加一些解释。
    猜你喜欢
    • 1970-01-01
    • 2015-02-07
    • 1970-01-01
    • 1970-01-01
    • 2012-11-23
    • 2011-11-06
    • 2011-12-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多