When trying to read tokens from a file in C++, I receive a seg fault. Just to play with some things, I tried reading over the file and just printing and surprisingly if you uncomment the code for the first file read and then read the second file everything works fine. If you leave the commented code commented, you receive the seg fault upon closing the first file. This could be an issue with the library on my school machine… The stack trace is below for the segfault (below the code itself). It’s also interesting to note that I do not have to do this ‘hack’ for each file I open, once the dummy-commented-code has been executed all subsequent streams open just fine.
int main()
{
ifstream myfile1;
ifstream myfile2;
int m; //number of elements in 1st file
int n; //number of elements in 2nd file
int counter = 0;
int x = 0;
int y = 0;
int theta = 0;
Minutiae* minutiae;
minutiae = new Minutiae(x,y,theta,0);
Minutiae* file1_minutiaes;
Minutiae* file2_minutiaes;
int num;
//Some Error is caused with segfault if i Try to fill the minutiae array and then close the file unless i do this first
/*////////////////////
myfile1.open("2a");
if (myfile1.is_open())
{
counter = 0;
while (!myfile1.eof() )
{
myfile1 >> x >> y >> theta;
minutiae = new Minutiae(x,y,theta,counter);
counter++;
}
myfile1.close();
}
else
{
cout << "unable to open file1" << endl;
return(0);
}
*/////////////////////////////////////
myfile1.open("2a");
if (myfile1.is_open())
{
counter = 0;
myfile1 >> m;
file1_minutiaes = new Minutiae[m];
while (!myfile1.eof() )
{
myfile1 >> x >> y >> theta;
//minutiae = new Minutiae(x,y,theta,counter);
//file1_minutiaes[counter] = *minutiae;
file1_minutiaes[counter] = *(new Minutiae(x,y,theta,counter));
counter++;
}
myfile1.close();
cout << "closing file1" << endl;
}
else
{
cout << "unable to open file1" << endl;
return(0);
}
////////////////////////////////
}
And the stack trace:
Starting program: /cs/student/dick_man_chini/Desktop/a.out
Program received signal SIGSEGV, Segmentation fault.
0x00c47a72 in _int_free () from /lib/libc.so.6
(gdb) up
#1 0x006d9342 in operator delete(void*) () from /usr/lib/libstdc++.so.6
(gdb) up
#2 0x006d939e in operator delete[](void*) () from /usr/lib/libstdc++.so.6
(gdb) up
#3 0x00686130 in std::basic_filebuf<char, std::char_traits<char> >::_M_destroy_internal_buffer() () from /usr/lib/libstdc++.so.6
(gdb) up
#4 0x006874d1 in std::basic_filebuf<char, std::char_traits<char> >::close() () from /usr/lib/libstdc++.so.6
(gdb) up
#5 0x006894a6 in std::basic_ifstream<char, std::char_traits<char> >::close() () from /usr/lib/libstdc++.so.6
(gdb) up
#6 0x08048e96 in main ()
(gdb) up
Initial frame selected; you cannot go up.
(gdb)
Thanks in advance.
The common process for reading files:
I suggest you use a
std::vectorinstead of an array:When processing data files, a good idea is to use dynamic containers.