reading a .txt file and storing in a struct

Hello! I'm trying to read a .txt file and store it's data in a structure so it shows the value if the user inputs the correct number from the command line

for example 111222333
would then read the text file and give you that students information

getting a segmentation fault

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 <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;

//Declare a structure for the record

struct Info
{
	int studentnumber;
	string lastname;
	string firstname;
	string major;
	string course;
	string section;
	string grade;
};

int main (int argc, char *argv[])
{
	int userinputnumber = atoi(argv[1]);
	string input;//to hold file input
	Info studentstuff; // to hold info about studen
	//open the file for input in binarymode
	fstream student("./student.txt",ios::in); //file stream object
	
	//if the file was sucessfully opened continue
	if (student)
	{
		//read an item usining ' ' as a delimiter
		getline(student, input , ' ');
	


	
		//store the last item read
		studentstuff.studentnumber = userinputnumber;	
		if(userinputnumber == studentstuff.studentnumber)
		{	
			getline(student,input,' ');
			studentstuff.lastname = input;
			
			getline(student,input,' ');
			studentstuff.firstname = input;
			
			getline(student,input,' ');
			studentstuff.major = input;
	
			cout<<studentstuff.firstname<<endl;
			cout<<studentstuff.lastname<<endl;
			cout<<studentstuff.major<<endl;
		}
		else
			student.close();
	}
	else
		cout <<"ERROR: Cannot open file.\n";
	return 0;
}


here is the student.txt file
1
2
3
4
5
6
7
8
9
10
444444444 Adams Sally CS FR
333222111   Samuels Ann EE SO
111222333 Charels  Don  CS SO
666666666 Daniels Tom UN JU
777766666 Williams  Al CE SE
555555555  Adams Sally EE FR
333333333  Nelson Mary CS SO
777777777 Dinh Tran   MA   JU
888888888 Galvez  Mara UN SE
009999888 Martin Dan  CS FR


in my own words my program starts by taking the commandline argument and giving it an int value. then compares that value with the first line from the .txt file. if it matches it then displays the rest of that line. maybe i've been yelling at my computer and need a break.
thanks
Looks fine to me. Are you accessing the .exe manually through the cmd (which you should for argv[] arguments) or are you building/running it through your IDE (which will cause a nasty error at startup since line 22 would be attempting to put an undefined element of argv[] into userinputnumber [perhaps this is a good place to check to see that there actually is an argument passed from cmd?])?

in cmd:
C:\PROJECTS\myproject.exe 444444444


Oh, also, you'll have to have a while loop from line 29 instead of 'if' because as is, only the very first line will be read through ie: "444444444 Adams Sally CS FR".

1 last thing. You could take advantage of the formatting and use student >> studentstuff.firstname; instead of getline to make things cleaner.
Last edited on
Topic archived. No new replies allowed.