简体   繁体   中英

C++ : Reading a “non-txt” file

I have this file u.user that has some data about users in the following format:

835|44|F|executive|11577
836|44|M|artist|10018

This is a text file basically but with the .user extension. I have been trying to use ifstream and getline methods to read one line at a time and store them in some sort of data structure, but it doesn't seem to be working. Error I get:

userbaseGM.cpp:15:18: error: variable âstd::ifstream myfileâ has initializer but incomplete type ifstream myfile ("u.user");

My code is:

int main() {
    //template<typename T> 
    //map<T,T> userMap;
    string line; 
    ifstream myfile ("u.user");
    if (myfile.is_open()) { 
        while ( getline (myfile,line) ) { 
            cout << line << '\n'; 
        } 
        myfile.close(); 
    } else cout << "Unable to open file";
}

Any suggestions on what I should be doing instead?

Thank you,

"incomplete type": Some header file you included provided a forward declaration of the class, but you never included the real definition.

Add

#include <fstream>

near the top of your file, and try again.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM