search

Home  >  Q&A  >  body text

C++读取文本文件

我想用c++读取文本文件,怎么每次只读取一行,然后将这一行放到数组中,再读下一行,依次类推。

PHP中文网PHP中文网2807 days ago678

reply all(2)I'll reply

  • 黄舟

    黄舟2017-04-17 13:15:40

    Is this okay? Instead of using an array, use string

    #include <iostream>
    #include <string>
    #include <fstream>
    #include <sstream>
    using namespace std;
    
    int main()
    {
        string s;
        ifstream input("your file here");
        while (getline(input, s))
            ;
    }

    reply
    0
  • PHP中文网

    PHP中文网2017-04-17 13:15:40

    Just use the getline function. For this function, reading from a file is the same as reading from the keyboard. After reading one line, process it and move on to the next line. Just end the reading by judging the stream status

    reply
    0
  • Cancelreply