c++ - 在c++中的特定文本行之间读取文本文件

标签 c++ string search getline

我目前正致力于将文件解析到程序内存中。

要解析的文件如下所示:

    file info
    second line of file info
    date

    # col1    col2    col3    col4   col5      col6      col7    col8     col9   col10    col11     col12     col13     col14  
    firstSetOfElements
    4
           2       1       1       0       3       1       2       3       0 -49.4377        0 0       -26.9356 -24.5221
           3       2       1       0       3       2       4       3       13.7527 -43.2619       0 0       -19.3462 -28.0525
           4       3       1       0       3       4       1       3       14.2459 43.5163       0 0       33.3506 15.2988
           5       4       1       0       3       2       1       4       49.4377 0       0 0       25.0818 38.3082


    # col1      col2      col3      col4      col5      col6
    secondSetOfElements
    1
         1 4 3 4 1 2

我正在尝试做的事情:

    file.open(FILENAME, ios::in); // Open file
    if (file.is_open()) 
    { 
            // Get the line number where "firstSetOfElements" is located. Store the line number.
            // Go to the line after that line, and store the integer listed there as a variable (`int noFirstElems`).
            // (this is the number of rows I will be parsing into the first array).
    // start parsing at the first line of the array (the line under the previous)     
         while (getline(file, firstLineOfFirstSetToParse)) //starting at the first row of data array elements, begin parsing into text file (this I've got handled).
         {
                 //add a row vector until you get to a blank line (which will be after 4 rows of data).
         }

在对“firstSetOfElements”数组执行此操作后,我将执行相同的操作以将“secondSetOfElements”数据解析为数组,如上所述。

我已经很好地解析了数据,但还没有找到我可以理解的资源来为我要解析的行设置开始/结束点。

提前致谢!

最佳答案

#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <vector>
using namespace std;

string trimmed(string& input)
{
    size_t firstNonWSIdx = input.find_first_not_of(" ");
    size_t lastNonWSIdx = input.find_last_not_of(" ");
    firstNonWSIdx = (firstNonWSIdx == string::npos ? 0 : firstNonWSIdx);
    return input.substr(firstNonWSIdx, lastNonWSIdx);
}

void resetStrStream(istringstream& sstr, string const& newInput)
{
    sstr.clear();
    sstr.str(newInput);
}

void readFile(char const* fileName, vector<vector<float> >& data)
{
    ifstream infile(fileName);
    string input;
    istringstream iss;
    while(infile)
    {
        getline(infile, input);
        string token = trimmed(input);
        if (token.compare("setOfElements") == 0)
        {
            getline(infile, input);
            resetStrStream(iss, trimmed(input));
            int arraySize;
            iss >> arraySize;
            vector<float> values;
            int i = 0;
            while(i++ < arraySize)
            {
                getline(infile, input);
                resetStrStream(iss, trimmed(input));
                float val;
                while (iss >> val)
                {
                   values.push_back(val);
                } 
            }
            data.push_back(values);
        }
    }    
}

void testData(vector<vector<float> >& data)
{
    for (int i = 0; i < data.size(); i++)
    {
        for (int j = 0; j < data[i].size(); j++)
        {
            cout << data[i][j] << " ";
        }
        cout << endl;
    }
}

int main()
{
    vector< vector<float> > data;
    readFile("textfile.txt", data);
    testData(data);
    return 0;
}

关于c++ - 在c++中的特定文本行之间读取文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22362732/

相关文章:

c++ - 为什么迭代器类型推导会失败?

java - 如何打印我的 Java 对象而不得到 "SomeType@2f92e0f4"?

ruby - 选择一个字符串,直到 100 个字符后的第一个句点

c++ - 在字符数组中使用 static_cast 来使用字符串函数

c++ - C++1y 中是否需要公共(public)类内类型定义?

c++ - 模板类型推导

c++ - 通过修改二进制搜索算法来改进它,使其在大量单词(单词列表)中搜索单词时工作得更快

regex - R:寻找不包含一组字符序列的单词

javascript - Chrome 扩展修改建议和搜索 url

c++ - boost 备忘单