Segmentation Fault Reading In .txt File

I am having an issue with a segmentation fault and I am pretty sure it occurs when I attempt to read in the .txt file. It is a family large file and I'm not sure how to fix this issue. I currently can't run a debugger but I am almost positive the program never enters this do while loop. Any suggestions?

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

#include <iostream>
#include <cstdlib>
#include <fstream>
#include "HashTable.h"

using namespace std;


int main(int argc, char** argv) {
    
    long tableSize;
    string username;
    string password;
    string firstName;
    string lastName;
    string email;
    string course1;
    string course2;
    long ID;
    ifstream file;
    ifstream searchFile;
    long searchID;
    long comparisons;
    long searchComparisons;
    long searchID2 = 0;
    float average;
    
    cout<<"ENTER THE SIZE OF THE HASH TABLE: ";
    cin>>tableSize;
    
    HashTable table(tableSize);
    
    file.open("STUDENTS.txt");
    do
    {
        file>>username;
        file>>password;
        file>>firstName;
        file>>lastName;
        file>>email;
        file>>ID;
        file>>course1;
        file>>course2;
        
        Student newStudent(username, password, firstName, lastName, email,
                course1, course2, ID);
        
        table.insert(newStudent);
        
    }while(!file.eof());
Last edited on
You should check the return from file.open();
> I am almost positive the program never enters this do while loop
If you don't enter the loop, then the problem is not in the reading.
However, it's a do-while loop, so it must be executed at least once.

Your reading is incorrect, in line 46 you process an student with data that may be invalid because you reached eof on line 44.
1
2
3
while(file>>username>>password>>firstname /*...*/){
   table.insert( Student(username, password, firstname, /*...*/) );
}



> I currently can't run a debugger
¿why not?


> #include "HashTable.h"
we don't have that file
@dafontem

Try adding cout << "test1\n" after line 32 and cout << "test2\n" after line 34. If you're positive it never enters the do-while loop but you still get a segmentation fault error, then the problem is most likely with line 32, and therefore with "HashTable.h" which you have not included.
Topic archived. No new replies allowed.