Using C++ ifstream extraction operator>> to read formatted data from a file

后端 未结 4 1513
栀梦
栀梦 2020-12-30 06:46

As my learning, I am trying to use c++ ifstream and its operator>> to read data from a text file using code below. The text file outdummy.txt has following contents:

4条回答
  •  悲哀的现实
    2020-12-30 07:22

    You can read file contents and use a Finite State Machine for parsing.

    Example:

    void Parse(const char* buffer, size_t length);
    size_t GetBufferSize();
    
    size_t bufferSize = GetBufferSize();
    char* buffer = new char[bufferSize];
    
    std::ifstream in("input.txt");
    while(in.getline(buffer, bufferSize)) {
        Parse(buffer, in.gcount());
    }
    

    Alternatively, you can use a tool like Flex to write your parser.

提交回复
热议问题