Repositioning the pointer to the input file

Would you please take a look to the lines 22, 23, 24.

I must issue all those 3 instructions in order to be able to read the file again from its beginning. If I leave out any one of those three line the getline that follows (line 27) will not find anything because the file was completely read with line 9.

With those 3 lines everything works. But Why?
Why closing/opening/repositioning?
Am I doing something conceptually wrong?

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
27
28
29
30
31
32
33
34
35
	ifstream inp_file;
		inp_file.open("C:/Garbage/rowsandcols.txt");	//input file
	ofstream out_file;
		out_file.open("C:/Garbage/columnonly.txt");	//output file

	// The following loop detects the size of the matrix
	r=0;
	nc=0;
	while (getline(inp_file, line)) {
		istringstream streamline (line);
		c=0;
		while (streamline >> item) {
			++c;
			if (c>nc) nc=c;
		}
		++r;
	}
	nr = r;

	matrix.resize(nr); for (r=0; r<nr; r++) matrix[r].resize(nc);
	
	inp_file.close();
	inp_file.open("C:/Garbage/rowsandcols.txt");	//input file
	inp_file.seekg(0);

	r=0;
	while (getline(inp_file, line)) {
		istringstream streamline (line);
		c=0;
		while (streamline >> item) {
			matrix[r][c] = item;
			++c;
		}
		++r;
	}
Try
inp_file.clear();
inp_file.seekg(0);
Helios, Thanks! I did not know about the function Clear().

Also, for those whoo might be interested, I found this posting that should clarify further:

http://www.cplusplus.com/forum/beginner/3602/
Last edited on
Topic archived. No new replies allowed.