Hi, sorry to post about a bus error I realize there are many answers on others threads but none of them have been able to help me make sense of this problem. Im fairly new to C and have tried everything I can think of. The program is supposed to read from a .txt file and count the numbers present until the special phrase "END" occurs and then terminate the program. I have isolated the problem to line 33. Which to my understanding should take the first character E and if present look to the next character and check for N and then again look to the next character for D, if so abort the program.

Any help or clarification on this problem would be great, thanks in advance.

1 char character[200];
2
3 //Open file grades.txt
4 FILE *input_file;
5 input_file = fopen("data.txt", "r");
6
7 //Input information from file
8 while (!feof(input_file) || abort == 1)
9 {
10 //Get single character from file
11 character = fgetc(input_file);
12
13 if (character == '0')
14 zero = zero +1;
15 else if (character == '1')
16 one = one + 1;
17 else if (character =='2')
18 two = two +1;
19 else if (character == '3')
20 three = three +1;
21 else if (character == '4')
22 four = four +1;
23 else if (character == '5')
24 five = five +1;
25 else if (character == '6')
26 six = six +1;
27 else if (character == '7')
28 seven = seven +1;
29 else if (character == '8')
30 eight = eight +1;
31 else if (character == '9')
32 nine = nine +1;
33 else if (character == 'E' && character[i+1] == 'N' && character[i+2] == 'D');
34 abort = 1;
35
36 i = i + 1;
37 }

Looks like you only get one character at a time so character[i+1] and character[i+2] are not loaded from the file. Unless 'E' can exist somewhere besides the end of good data, just checking for 'E' will make it work. Why not leave END out of the file and use the "End Of File" test.

Thanks for the response, I would have just used the EOF as i agree that is much easier. But having END in the file was a requirement. And I have resolved the problem as well so this post can be taken down. Thanks again for the help though.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.