简体   繁体   English

读取某些行上带有前导制表符的制表符分隔文本文件

[英]Reading a tab delimited text file with leading tabs on some lines

I am working to build a console-based spreadsheet app that takes in a UTF-8 encoded text file as input and outputs the results to the console.我正在构建一个基于控制台的电子表格应用程序,它将 UTF-8 编码的文本文件作为输入并将结果输出到控制台。

Column values are separated by tabs and each new line is a new row.列值由制表符分隔,每个新行都是一个新行。 I am having some issues reading in the tab-delimited input text file where some of the lines (rows) are starting with a tab indicating that there is no value in the first column(s).我在以制表符分隔的输入文本文件中读取时遇到一些问题,其中某些行(行)以制表符开头,表明第一列中没有值。 I would like to just extract the "filled" cells and use the data elsewhere in the program and discard or ignore the "empty" cells.我只想提取“填充”单元格并使用程序中其他地方的数据并丢弃或忽略“空”单元格。 Using the '\\t' delimiter in the getline() function does not seem to ignore these leading tabs.getline()函数中使用 '\\t' 分隔符似乎不会忽略这些前导选项卡。 Thank you ahead of time for any help or code suggestions.提前感谢您提供任何帮助或代码建议。

Example Input:示例输入:

1 \t 2
\t 3
\t \t =A1+B1+B2 

The simple code I've been using is below:我一直在使用的简单代码如下:

#include <iostream>
#include <stream>
#include <string>

// Variable declarations
std::ifstream sheetFile;
std::string input;

int main(int argc, char *argv[])
{
    sheetFile.open(argv[1]);
    while (getline(sheetFile, input, '\t'))
    {
        std::cout << input << std::endl;
    }

    sheetFile.close();
    return 0;
}

And the return to the console is:返回控制台是:

1
2

3


=A1+B1+B2

You can use multiple std::getline() calls - one in the loop to read each line delimited by \\n , and then put each line into a std::istringstream and use std::getline() on thaat stream to parse each column delimited on \\t , eg:您可以使用多个std::getline()调用 - 循环中的一个调用以读取由\\n分隔的每一行,然后将每一行放入std::istringstream并在 thaat 流上使用std::getline()来解析每个以\\t分隔的列,例如:

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>

int main(int argc, char *argv[])
{
    // Variable declarations
    std::ifstream sheetFile(argv[1]);
    std::string line, input;

    while (std::getline(sheetFile, line))
    {
        std::istringstream iss(line);
        while (std::getline(iss, input, '\t'))
        {
            if (!input.empty())
                std::cout << input << std::endl;
        }
    }

    return 0;
}

Alternatively, using a single std::getline() , you can use the std::ws stream manipulator to ignore leading whitespace on each line, which will include \\t and \\n characters:或者,使用单个std::getline() ,您可以使用std::ws流操纵器忽略每行的前导空格,其中包括\\t\\n字符:

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>

int main(int argc, char *argv[])
{
    // Variable declarations
    std::ifstream sheetFile(argv[1]);
    std::string input;

    while (std::getline(sheetFile >> std::ws, input, '\t'))
    {
        std::cout << input << std::endl;
    }

    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM