'Segmentation Fault'

closed account (9GEqko23)
Hello, I was just wondering why I keep getting a segmentation fault when I run my program. What this function does is collect text from the text file and stores them into a class.
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
void AccTree::loadRecords(ifstream& inFile){

	inFile.clear();
	inFile.seekg(0, inFile.beg);
	
	Account record;

	while(!inFile.eof()){
		int tempBSB;
		int tempAcc;
		char tempName[30];
		char tempAddress[50];
		int tempPhone;
		char tempDate[10];
		float tempBalance;

		//Collect BSB
		char temp[7];
		inFile.getline(temp,7 ,'\t' );
		tempBSB = atoi(temp);

		//Collect Account Number
		char temp2[8];
		inFile.getline(temp2,8 ,'\t' );
		tempAcc = atoi(temp2);

		//Collect Name
		inFile.getline(tempName,30 ,'\t' );

		//Collect Address
		inFile.getline(tempAddress,50 ,'\t' );

		//Collect Phone
		char temp3[10];
		inFile.getline(temp3,10 ,'\t' );
		tempPhone = atoi(temp3);

		//Collect Date
		inFile.getline(tempDate,10 ,'\t' );

		//Collect Balance
		char temp4[7];
		inFile.getline(temp4,7 ,'\t' );
		tempBalance = atof(temp4);

	record.setAccount(tempBSB, tempAcc, tempName, tempAddress, tempPhone, tempDate, tempBalance);

		insert(record);
	}
}
closed account (48T7M4Gy)
http://stackoverflow.com/questions/2346806/what-is-segmentation-fault
Notice that the the terminating '\0' is add to the string. Hence if you have an array with 7 char only 6 char can be entered. If you enter 7 it will fail. All following arrays will remain uninitialized and invalid data is passed to the functions.

Read:

http://www.cplusplus.com/reference/istream/istream/getline/
Hi,

My 1 cent worth :+)

Don't loop on eof

Instead, loop on the filestream itself.
Topic archived. No new replies allowed.