[Solved] C++: Trying to read from a file line by line, saving into a vector and then printing the vector prints nothing


You could – after skipping the line count – read in each individual character and compare it to '0' or '1'. See the following code:

int main() {

    vector<bool> bits;
    ifstream f(DATAFILE);
    if (f.is_open()) {
        int dummy;
        f >> dummy;
        char c;
        while (f >> c) {
            if (c == '1') {
                bits.push_back(true);
            }
            else if (c=='0') {
                bits.push_back(false);
            }
        }
        f.close();
        for(int i = 0; i < bits.size(); ++i){
            cout << bits[i] << " ";
        }
    }
    return 0;
}

2

solved C++: Trying to read from a file line by line, saving into a vector and then printing the vector prints nothing