Sumit
Sumit

Reputation: 35

Taking input from files in c++?

I have a file i need to take input from in my program in c++.. File structure is

100
150
245 467 367 367

Using get() only reads first line...using get() again does not help.. Pls suggest best method for it. Thanks

Upvotes: 0

Views: 570

Answers (1)

csnate
csnate

Reputation: 1641

See: http://en.cppreference.com/w/cpp/io/basic_ifstream

Try something like this:

#include <fstream>
#include <iostream>
#include <string>

using namespace std;

int main()
{
    ifstream stream("file.txt");
    string foo;

    while(stream >> foo)
            cout << foo << endl;

    return 0;
}

Output:

100
150
245
467
367
367

You can use the stream operators to extract one number at a time.

Update your question if this doesn't answer it as its kind of hard to tell what you're trying to do.

Upvotes: 1

Related Questions