I need to know WHY this works?

So this simple solution reads the file "level1.lbh" and prints out the frst two lines of text in that file. My questiuon is WHY? I am totally new to programming

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <fstream>
#include <stdlib.h>

std::fstream inFile;

int Height, Width;

int main()
{
	inFile.open("level1.lbf", std::fstream::in);

	inFile >> Height; 
	inFile >> Width;
	
	inFile.close();

	std::cout << "Reading level size from level1.lbf..." << std::endl;
	std::cout << "Height " << Height << std::endl;
	std::cout << "Width " << Width << std::endl;

	system("PAUSE");

	return 0;

}
Now you have to read a bit.

On line 5 it says that "inFile is a std::fstream
http://www.cplusplus.com/reference/fstream/fstream/
On line 11 there is some open( ... )
http://www.cplusplus.com/reference/fstream/fstream/open/
Lines 13 and 14 have a >>
http://www.cplusplus.com/reference/istream/istream/operator%3E%3E/

Or perhaps it would be better for you to start with:
http://www.cplusplus.com/doc/tutorial/
http://www.cplusplus.com/doc/tutorial/basic_io/
http://www.cplusplus.com/doc/tutorial/files/


The line 22 is not so good. See:
http://www.cplusplus.com/forum/beginner/1988/
http://www.cplusplus.com/articles/j3wTURfi/


The program does not exactly read "two lines of text". It reads two integers. It is a mere (happy?) coincidence if the file "level1.lbf" happens to contain one integer in each of the two first lines and no other characters (save whitespace).
Topic archived. No new replies allowed.