Unable to read more than 1 file unless nothing is done prior to their reading

As part of a project I am doing I need to read two files of data into my editor.

If I try to read both of them in my main() function before doing anything else then they both read in fine.

However, if I have anything written before that then I only the first fstream works, and the second does not read in. So, for example, I cannot write a function before main().

Here is the first part of my code (reading in the data):

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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
  #include <iostream>
  #include <cmath>
  #include <cstdlib>
  #include <string>
  #include <fstream>
  #include <vector>
  #include <algorithm>
  #include <math.h>
  #include "header1.h"
  #include "header2.h"

  using namespace std;

int main() {
    
    string path = "/Users/Olly/Documents/BScProject/WeakLensing/";
    string mainFile = path + "W1.mosaic.sources.ascii";
    ifstream in(mainFile.c_str(), ios::in);
    
    if (!in) {
        cerr << "Cannot open file " + mainFile << endl;
        exit(1);
    }
    
    vector <double> Pos1gal, Pos2gal, Pos1deg, Pos2deg, E1, E2, Z, W, void1, void2, void3, void4, void5, void6, void7;
    while (!in.eof()) {
        double x, y, xdeg, ydeg, e1, e2, z, w, v1, v2, v3, v4, v5, v6, v7;
        in >> x >> y >> xdeg >> ydeg >> e1 >> e2 >> z >> w >> v1 >> v2 >> v3 >> v4 >> v5 >> v6 >> v7;
        Pos1gal.push_back(x);
        Pos2gal.push_back(y);
        Pos1deg.push_back(xdeg);
        Pos2deg.push_back(ydeg);
        E1.push_back(e1);
        E2.push_back(e2);
        Z.push_back(z);
        W.push_back(w);
        void1.push_back(v1);
        void2.push_back(v2);
        void3.push_back(v3);
        void4.push_back(v4);
        void5.push_back(v5);
        void6.push_back(v6);
        void7.push_back(v7);
    }
    
    in.close();
    
    string mainFile2 = path + "hpeaks_type_C_ord_1_th_0.5_0.5_0.5_0.5_1_SNmin_2.5_masked_0.3.dat";
    ifstream in2(mainFile2.c_str(), ios::in);
    
    if (!in) {
        cerr << "Cannot open file " + mainFile2 << endl;
        exit(1);
    }
    
    int Nclusters = 354;
    
    vector <double> PeakNum, PeakHierarchicalMass, PeakStoN, FractionOfMaskedAreaInsideAperture, Pos1cluster, Pos2cluster, PeakRad;
    for (int i = 0; i < Nclusters; i++) {
        int peakNum, xx, yy, radius;
        double mass, StoN, frac;
        in2 >> peakNum >> mass >> StoN >> frac >> xx >> yy >> radius;
        if (mass > 5.e+14) {
            PeakNum.push_back(peakNum);
            PeakHierarchicalMass.push_back(mass);
            PeakStoN.push_back(StoN);
            FractionOfMaskedAreaInsideAperture.push_back(frac);
            Pos1cluster.push_back(xx);
            Pos2cluster.push_back(yy);
            PeakRad.push_back(radius);
        }
    }
    
    in2.close();


Here are the header files:

1
2
3
4
5
6
7
8
9
10
11
12
  double max(vector <double> vec) {
    
    double max = vec[0];
    for (int i = 0; i < vec.size(); i++) {
        if (vec[i] > max) {
            max = vec[i];
        }
    }
    
    return max;
    
}


and

1
2
3
4
5
6
7
8
9
10
11
12
  double min(vector <double> vec) {
    
    double min = vec[0];
    for (int i = 0; i < vec.size(); i++) {
        if (vec[i] < min) {
            min = vec[i];
        }
    }
    
    return min;
    
}


The in2 stream is what doesn't get read in, and it doesn't matter which file I include with in2 - it is simply the second file that does not get read in.

You will be able to see here that I did not write any functions in this file, but instead wrote them as separate header files (shown above (these particular functions compute the max/min of the vectors that have been read in)) and included those header files. Not using header files and writing the functions before main() results in the same problem.

So here is what I have tried:

1. Switching the files used within 'in' and 'in2'. (Only 'in' works, so the issue is irrespective of the file.)

2. Writing header files and including them instead of writing the functions before main(). (And vice versa.) Both don't work.

Here is the only way both files can be read in without an issue: If I write all of my functions within main() after the files have been read in. Obviously this is utterly unsustainable. (Possible, but extremely messy.)

SOLVED
Me being blind: see jib's comment (second down) for the issue.
Last edited on
> and the second does not read in.
¿what does that mean?
¿the file doesn't open, the vectors remain empty, the program freezes, it crashes? ¿what?


> You will be able to see here that I did not write any functions in this file,
> but instead wrote them as separate header files
> and included those header files.
you should realise that we don't have those headers files so we can't compile your program.
we also don't have the input files to test it.

Provide a testcase that does reproduce your issue.
Look at this snippet:
1
2
3
4
 
    ifstream in2(mainFile2.c_str(), ios::in);
    
    if (!in) {


Why are you testing the "in" stream? You have already closed this stream and are now trying to work with the "in2" stream.

@ne555
1. When I say the second one does not get in, I mean that my error message shows up (i.e. it is true that ' !in '. So yes, the vectors remain empty.

2. Apologies for not being clear about the header files, I have edited my question to include the header files and what they are used for. They work fine in my case, because they are used on the first data file that I read, but there would be issues if I needed them for my second data file, since the vectors are empty.

@jib
My gosh, I've been staring at this code for days and hadn't spotted that. Haha. Thanks - sorry for wasting everyones time!!
Last edited on
By the way an ifstream is always an input file (ios::in) and unless you're using an outdated compiler that is not using one of the more current C++ standards (C++11 or higher) you shouldn't need to use the c_str() method.
Topic archived. No new replies allowed.