1. AAAA needs 5 characters of storage, not 4
2. Unlike fgets, the count you supply to scanf does not take into account the need for a \0.
3. Trying to use sizeof() with scanf is horrible. There is no easy way to automatically tell scanf what the size of the array is.
4. while(feof(input)== 0) using feof() to control a loop is bad.
http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1046476070&id=1043284351
Try something like
char a[5]; // etc
char buff[BUFSIZ];
while ( fgets( buff, sizeof buff, input ) != NULL ) {
if ( sscanf( buff, "%4s %4s %d", a, b, &c ) == 3 ) {
}
}
Salem
Posting Sage
11,531 posts since Dec 2005
Reputation Points: 5,875
Solved Threads: 953
Skill Endorsements: 27
Perhaps you didn't read my post.
You're putting 5 characters into an array with only room for 4
Where does the extra one go?
Two guesses
- over the return address from the function
- over another variable
Neither of these is going to make your code nice and functional.
Salem
Posting Sage
11,531 posts since Dec 2005
Reputation Points: 5,875
Solved Threads: 953
Skill Endorsements: 27
Because when you are reading data from the file, it is read in the form of string and in your case, its C string.
The thing about C strings is that other than the characters it holds, it reserves a extra space at the end of the character array for the null character ('\0') which signifies the end of the C style or character array style string.
So when you are reading "AAAA" in your case, its actually is :
AAAA'\0'
As you can see the last character is the null character which is automatically appended at the end of character arrays so that they can be treated as strings. This null character even doesn't show up when you query the length of the string using strlen( ) and hence is a common mistake made by beginners to assume that C strings only require the size equivalent to the number of characters they contain.
So in your previous code, you were using an array of size 4 which caused the null character to be copied to a location which doesn't belong to you and it overwrote the memory which you don't own. This normally leads to what is known as "Segmentation Error" i.e. modifying or touching memory which doesn't belong to you.
Hence changing the size from 4 to 5 caused your code to function correctly. Also consider changing from three sscanf( ) statements to a single fgets( ) and sscanf( ) which leads to a more robust and elegeant solution.
~s.o.s~
Failure as a human
12,220 posts since Jun 2006
Reputation Points: 3,307
Solved Threads: 783
Skill Endorsements: 55
Question Answered as of 6 Years Ago by
Salem
and
~s.o.s~ > P.S. Also, I noticed your status, maybe you can tell me what's wrong with void main()?
http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1044841143&id=1043284376
You basically have two choices
- learn dialect C, which is specific to your current compiler, and which has to be re-learnt for every new compiler you come across. And also endure the constant nagging of many people to do the "right thing".
- learn ANSI C, which will work exactly the same on any ANSI-C compiler you will come across.
You've already seen that compilers do not trap every possible problem, so you should never regard the absense of warnings as the absense of bugs (or poor portability, style or what have you).
Salem
Posting Sage
11,531 posts since Dec 2005
Reputation Points: 5,875
Solved Threads: 953
Skill Endorsements: 27