reading file from last position

Hi

The code successfully reads a file, line by line, up to a certain point, then does some processing.
The code should restart reading from the line, one line beyond it stopped at.

How can I go about getting this done?

The code appears as follows:
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
#include <iostream>
#include "graph.h"
#include <fstream>

int main(int argc, char *argv[]) {

  if (argc != 2) { // argc should be 2 for correct execution
    std::cout<<"usage: "<< argv[0] <<" <filename>\n";
    return 1;
  }

  std::ifstream infile(argv[1]);
  Graph<int> g;

  std::string line;  
  while (std::getline(infile, line)) {
    if (line != "S") {
      std::istringstream edge{line};
      g.update_row_col_map(edge);
    }
    else
      break;
  }

  std::cout << g;

  return 0;

}


The input file and successful output thus far is :
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
36
37
38
39
40
41
42
$ cat edges.dat 
1 2
2 3
3 1
4 1
4 5
5 3
5 6
5 7
5 8
6 4
7 6
8 9
9 10
S
Q 2 6
Q 4 10
Q 1 2
Q 1 10
$ ./test_graph edges.dat 
Row by columns 
1| 2   
2| 3   
3| 1   
4| 1   5   
5| 3   6   7   8   
6| 4   
7| 6   
8| 9   
9| 10   

Columns by rows 
1 | 3   4   
2 | 1   
3 | 2   5   
4 | 6   
5 | 4   
6 | 5   7   
7 | 5   
8 | 5   
9 | 8   
10| 9   


The code should restart reading at line 15, which is "Q 2 6".
Last edited on
Well, if you don't use break at line 22, it will continue processing at the next line. Maybe you should put some additional code, or possibly a function call there instead?

Or just add some more code afterwards, the stream does remember its current position, so it should not present a problem either way.
Last edited on
Topic archived. No new replies allowed.