Segmentation Fault in an input/output array

when I try to run the code, I get a segmentation fault core dumped
it returns 139 (0x8B)
the file it is pulling from is a list of numbers, but without the string, the output ends up going from values of ~70,000 to values around 50

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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <iostream>
#include <fstream>
#include <cstdio>
#include <cstdlib>
#define MAX 29656
using namespace std;
string el[MAX];
ifstream file1("results.txt");
ofstream file2("Energy1.txt");
ofstream file3("Px1.txt");
ofstream file4("Py1.txt");
ofstream file5("Pz1.txt");

ofstream file6("Energy2.txt");

ofstream file7("Px2.txt");
ofstream file8("Py2.txt");

ofstream file9("Pz2.txt");

void input()
{
   for (int n=1; n<=MAX; n++)
   {
      el[n]=file1.get();
    }
}
void output()
{
    for (long long i=1; i<=MAX; i++)
    {
       int j=i%8;
       if (j==1)
       {
           string E1= el[i];
            file2 <<E1 <<endl;
        }
        if (j==2)
        {
           string Px1= el[i];
           file3 <<Px1 <<endl;
        }
        if (j==3)
        {
            string Py1= el[i];
            file4 <<Py1 <<endl;
         }
        if (j==4)
        {
             string Pz1= el[i];
             file5 <<Pz1 <<endl;
         }
        if (j==5)
        {
            string E2= el[i];
            file6 <<E2 <<endl;
        }
       if (j==6)
       {
            string Px2= el[i];
           file7 <<Px2 <<endl;
        }

       if (j==7)
       {
       string Py2=el[i];
       file8 <<Py2 <<endl;
       }
      if (j==0)
      {
         string Pz2=el[i];
         file9 <<Pz2 <<endl;
      }
  }
}
int main()
{
  while(file1)
   {
      cout <<"open" <<endl;
      input();
      output();
     file2.close(); 
     file3.close();
     file4.close();
     file5.close();
     file6.close();
     file7.close();
     file8.close();
     file9.close();
    return 0;
  }

     cout<< "did not open" <<endl;
    return 0;
 }
Last edited on
That's pretty much unreadable as presented. Can you:
1. Format your code using the format tags and ensure it's indented in a reasonable manner.
2. Remove calls to open/close on streams, it's wrong and distracting.
3. Don't check a stream if it's open, check if it's good. For example:
1
2
3
4
std::ifstream f1(filename);
while (f1) // test for good
{
   // do stuff with f1 

Topic archived. No new replies allowed.