Read a string line by line using c++

前端 未结 5 1611
不思量自难忘°
不思量自难忘° 2020-12-15 03:30

I have a std::string with multiple lines and I need to read it line by line. Please show me how to do it with a small example.

Ex: I have a string

相关标签:
5条回答
  • 2020-12-15 04:02

    this would help you : http://www.cplusplus.com/reference/iostream/istream/getline/

    0 讨论(0)
  • 2020-12-15 04:03
    #include <sstream>
    #include <iostream>
    
    int main() {
        std::istringstream f("line1\nline2\nline3");
        std::string line;    
        while (std::getline(f, line)) {
            std::cout << line << std::endl;
        }
    }
    
    0 讨论(0)
  • 2020-12-15 04:08

    There are several ways to do that.

    You can use std::string::find in a loop for '\n' characters and substr() between the positions.

    You can use std::istringstream and std::getline( istr, line ) (Probably the easiest)

    You can use boost::tokenize

    0 讨论(0)
  • 2020-12-15 04:21

    I was looking for some standard implementation for a function which can return a particular line from a string. I came across this question and the accepted answer is very useful. I also have my own implementation which I would like to share:

    // CODE: A
    std::string getLine(const std::string& str, int line)
    {
        size_t pos = 0;
        if (line < 0)
            return std::string();
    
        while ((line-- > 0) and (pos < str.length()))
            pos = str.find("\n", pos) + 1;
        if (pos >= str.length())
            return std::string();
        size_t end = str.find("\n", pos);
        return str.substr(pos, (end == std::string::npos ? std::string::npos : (end - pos + 1)));
    }
    

    But I have replaced my own implementation with the one shown in the accepted answer as it uses standard function and would be less bug-prone..

    // CODE: B
    std::string getLine(const std::string& str, int lineNo)
    {
        std::string line;
        std::istringstream stream(str);
        while (lineNo-- >= 0)
            std::getline(stream, line);
        return line;
    }
    

    There is behavioral difference between the two implementations. CODE: B removes the newline from each line it returns. CODE: A doesn't remove newline.

    My intention of posting my answer to this not-active question is to make others see possible implementations.

    NOTE:

    I didn't want any kind of optimization and wanted to perform a task given to me in a Hackathon!

    0 讨论(0)
  • 2020-12-15 04:27

    If you'd rather not use streams:

    int main() {
      string out = "line1\nline2\nline3";
      size_t start = 0;
      size_t end;
      while (1) {
        string this_line;
        if ((end = out.find("\n", start)) == string::npos) {
          if (!(this_line = out.substr(start)).empty()) {
            printf("%s\n", this_line.c_str());
          }
    
          break;
        }
    
        this_line = out.substr(start, end - start);
        printf("%s\n", this_line.c_str());
        start = end + 1;
      }
    }
    
    0 讨论(0)
提交回复
热议问题