Very very strange situation with reading a file

Dear all,

I would kindly ask for some help in debugging the code provided below, which reads data from a file and puts it into several vectors.

Something very strange happens: if I try to read the file up to the last line, I get an runtime error, while if I try to read the file up to the second last line, I do not get the error.

However, if I try to read the file up to the last line, it READS IT CORRECTLY, as you can see that it prints out the right values.

Following there is the code and the file that I am trying to read.

I would be grateful if anyone has some clue about what is going on here.
My felt thanks in advance.


Code:
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include<stdio.h>
#include<iostream>
#include<fstream>
#include<vector>
#include<set>
#include<queue>
#include<cmath>


int main()
{
  std::ifstream fp;
  fp.open("link1.dat");
  int N_edges;
  fp >> N_edges;
  std::vector<double> pc_entry(N_edges);
  std::vector<double> edge_volume(N_edges);
  std::vector<double> edge_radius(N_edges);
  std::vector<double> edge_SF(N_edges);
  std::vector<std::vector<int> > neigh;
  std::vector<std::vector<int> > edge_idx;
  
  {
    int n1, n2, e;
    double xx;
    for (int i=0; i<N_edges; i++)
    {
      fp >> e;     //edge index
      fp >> n1;    //first node of e
      fp >> n2;    //second node of e
      n1++;
      n2++;
      int max = (n1>n2) ? n1 : n2;
      if (neigh.size() < max+1) 
      {
        neigh.resize(max+1);
        edge_idx.resize(max+1);
      }
      neigh[n1].push_back(n2);
      edge_idx[n1].push_back(e);
      neigh[n2].push_back(n1);
      edge_idx[n2].push_back(e);

      fp >> edge_radius[e];
      fp >> edge_SF[e];
      fp >> xx; //wasted variable
      printf("edge %d correctly read\n", i+1);
      std::cout << e << ", " << n1  << ", "  << n2  << ", "  << edge_radius[e]  << ", " << edge_SF[e] << ", " << xx << std::endl;
    }

    printf("size = %d\n", neigh.size());
    fp.close();
    printf("file LINK1.DAT correctly closed\n");


    
    
  }

  return 0;
}



Following, there is the file "link1.dat" that I am trying to open.

1
2
3
4
5
6
7
8
9
10
11
10
1 1 8 2.171563e-06 1.837725e-02 3.939158e-05
2 3 17 1.067390e-05 3.612572e-02 1.047469e-04
3 2 9 1.166078e-05 3.280536e-02 3.207107e-05
4 4 26 1.236551e-05 3.143551e-02 2.054519e-04
5 7 18 3.430566e-05 2.821557e-02 1.109601e-04
6 12 18 9.582478e-06 2.847688e-02 4.161835e-04
7 12 43 5.816782e-06 4.352799e-02 4.141623e-04
8 12 16 2.036719e-05 4.194844e-02 2.629832e-04
9 12 11 1.829523e-05 3.096140e-02 4.543953e-04
10 11 47 2.563568e-05 4.001301e-02 3.140850e-04
Last edited on
if I try to read the file up to the last line, I get an runtime error


However, if I try to read the file up to the last line, it READS IT CORRECTLY


Make your mind up dude :)
The purpose is obviously to read the whole file.
I do not understand why I get the error, and, even more weird, why the error disappear if I try to read only part of the file
The problematic write is here:
 
fp >> edge_radius[e];


e is ranged from 1 to 10 in link1.dat, but edge_radius is indexed 0-9.
At the last item, you are trying to write to edge_radius[10].
You'll also notice you've skipped edge_radius[0]
Dear SneakySnake,

I thank you for pointing out what the problem was.
Now it works
Topic archived. No new replies allowed.