Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read integer value from file in C++

How can read integer value from file? For example, these value present in a file:

5 6 7

If I open the file using fstream then how I can get integer value?

How can read that number and avoid blank space?


2 Answers

ifstream file;
file.open("text.txt");

int i;

while (file >> i) {
   cout << i << endl;
}
ifstream f(filename);

int x, y, z;
f >> x >> y >> z;
like image 30
Fred Foo Avatar answered Sep 21 '25 05:09

Fred Foo