简体   繁体   中英

C++ Using fstream to read from a file

I use Putty for coding, so my question, while general in scope, is asked from the perspective of that program.

How do you take a file from the command line and read it line for line into the main program so it can then be parceled out and manipulated in functions? I understand that you use the fstream class, but I'm not sure at the correct procedure for actually reading lines from the file

This is a simple sample:

ifstream in("file.txt");

if (!in.is_open())
{
    cout << "Error - cannot open the file." << endl;
    return 0;
}

string line;
while (getline(in, line))
{
    cout << line << endl;

    ...

}

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