How to Read from a Text File, Character by Character in C++

后端 未结 8 1818
孤街浪徒
孤街浪徒 2020-12-06 10:17

I was wondering if someone could help me figure out how to read from a text file in C++, character by character. That way, I could have a while loop (while there\'s still te

8条回答
  •  无人及你
    2020-12-06 10:38

    Here is a c++ stylish function your can use to read files char by char.

    void readCharFile(string &filePath) {
        ifstream in(filePath);
        char c;
    
        if(in.is_open()) {
            while(in.good()) {
                in.get(c);
                // Play with the data
            }
        }
    
        if(!in.eof() && in.fail())
            cout << "error reading " << filePath << endl;
    
        in.close();
    }
    

提交回复
热议问题