简体   繁体   English

如何以这种特定方式读取和处理文件中的文本?

[英]How to read and process text from a file in this specific way?

I have a question regarding some code to process some names or numbers from a file I'm reading. 我有一个问题,涉及一些代码来处理正在读取的文件中的某些名称或数字。 So the text in the file looks like this: 因此,文件中的文本如下所示:

Imp;1;down;67
Comp;3;up;4
Imp;42;right;87

As you can see , there are 3 lines with words and numbers delimited by the character ';' 如您所见,共有3行,用单词“;”分隔单词和数字。 . I want to read each line at a time, and split the entire string in one line into the words and numbers , and then process the information (will be used to create a new object with the data). 我想一次读取每一行,然后将整个字符串分成一行,分成单词和数字,然后处理信息(将用于创建带有数据的新对象)。 Then move on to the next line, and so on, until EOF. 然后继续下一行,依此类推,直到EOF。

So, i want to read the first line of text, split it into an array of strings formed out of the words and numbers in the line , then create an object of a class out of them. 因此,我想读取文本的第一行,将其拆分为由该行中的单词和数字组成的字符串数组,然后从中创建一个类的对象。 For example for the first line , create an object of the class Imp like this Imp objImp(Imp, 1, down, 67) . 例如,对于第一行,创建一个Imp类的对象,例如Imp objImp(Imp, 1, down, 67)

In Java i did the same thing using information = line.split(";")' (where line was a line of text) and then used information[0] , information[1] to access the members of the string array and create the object. 在Java中,我使用information = line.split(";")' (其中line是一行文本)进行了同样的操作,然后使用information[0]information[1]访问字符串数组的成员并创建物体。 I`m trying to do the same here 我想在这里做同样的事情

Don't use char array for buffer, and don't use std::istream::eof . 不要使用char数组作为缓冲区, 也不要使用std::istream::eof That's been said, let's continue in solving the problem. 话虽这么说,让我们继续解决问题。

std::getline is simmilar to std::istream::getline , except that it uses std::string instead of char arrays. std::getlinestd::istream::getline类似,不同之处在于它使用std::string代替char数组。

In both, the parameter delim means a delimiting character, but in a way that it's the character, which when encountered, std::getline stops reading (does not save it and discards it). 在这两种情况下,参数delim表示一个定界字符,但在某种意义上来说,它就是该字符,当遇到std::getline时,该字符将停止读取(不保存并丢弃它)。 It does not mean a delimiter in a way that it will magically split the input for you between each ; 这并不意味着以一种分隔符的方式,它将神奇地为您分割输入; on the whole line. 在整个生产线上。

Thus, you'll have to do this: 因此,您必须执行以下操作:

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

...

std::ifstream myFile("D:\\stuff.txt"); // one statement

if (myFile.is_open()) {
    std::string line;

    while (std::getline(myFile, line)) { // line by line reading
        std::istringstream line_stream(line);
        std::string output;

        while (std::getline(line_stream, output, ';')) // line parsing
            std::cout << output << std::endl;
    }
}

We construct a std::istringstream from line , so we can parse it again with std::getline . 我们从line构造了一个std::istringstream ,所以我们可以用std::getline再次解析它。

One other (slightly different) alternative: 另一种(略有不同)替代方法:

/*
 * Sample output:
 *   line:Imp;1;down;67
 *     "Imp", "1", "down", "67"
 *   line:Comp;3;up;4
 *     "Comp", "3", "up", "4"
 *   line:Imp;42;right;87
 *     "Imp", "42", "right", "87"
 *   line:Imp;42;right;87
 *     "Imp", "42", "right", "87"
 */
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

void split(const std::string &s, char delim, std::vector<string> &fields)
{
    fields.clear();
    std::stringstream ss(s);
    std::string item;
    while (std::getline(ss, item, delim)) {
        fields.push_back(item);
    }
}

void print (std::vector<string> &fields)
{
  cout << "  ";
  for (size_t i = 0; i < fields.size() - 1; i++)
    cout << "\"" << fields[i] << "\", ";
  cout << "\"" << fields[fields.size()-1] << "\"" << endl;
}

int main ()
{
  std::ifstream fp("tmp.txt");
  std::string line;
  while (!fp.eof()) {
    fp >> line;
    cout << "line:" << line << endl;
    std::vector<std::string> fields;
    split(line, ';', fields);
    print(fields);
  }
  fp.close();
  return 0;
}

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

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