user2766569
user2766569

Reputation: 1

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

Upvotes: 0

Views: 308

Answers (1)

masoud
masoud

Reputation: 56529

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;

    ...

}

Upvotes: 1

Related Questions