Weird behavior counting lines from a file?

Any time you see strange behavior like this, you should start looking for memory overrun errors. Typical symptoms include.

Up vote 0 down vote favorite share g+ share fb share tw.

Looking for a way to read arguments from a configuration file I found a nice way to do it but the thing is that something really weird happens when I try to count the lines. This is the code: FILE *file = fopen("config", "r"); char line100; int linenum = 0; //int foo; Uncomment and it starts to working, doesn't matter if you rename it. While(fgets(line, sizeof(line), file)!

= NULL) { char option4; char arg100; if (linelinenum == '#') continue; linenum++; if (sscanf(line, "%s %s", option, arg)! = 2) fprintf(stderr, "Syntax error, line %i\n", linenum); The config file looks like this: #config file option1 option2 option3 So the result is: Syntax error, line 1 Syntax error, line 0 Syntax error, line 0 But if I declare an int variable with any name before while loop it starts to working! The result is: Syntax error, line 1 Syntax error, line 2 Syntax error, line 3 What in the world is happening here?

My mind is gonna blow, maybe it's something dumb but I don't see any reason for this. C algorithm file gcc read link|improve this question edited Feb 21 at 21:13Alex Reynolds27.7k954109 asked Feb 21 at 21:06gmol152.

– Drew Galbraith Feb 21 at 21:20 You're right I just changed linelinenum for line0 thanks a lot. – gmol Feb 21 at 21:34.

Any time you see strange behavior like this, you should start looking for memory overrun errors. Typical symptoms include: Code that works or stops working if you add or remove unused variables. Variables that change for no apparent reason.

Code that sometimes runs normally and crashes other times, using the same input. Code that produces different results between invocations, using the same input. In this case, as the other answers point out, you have two potential memory-related errors: The line if (linelinenum == '#') continue; should be if (line0 == '#') continue; Otherwise the index will be invalid when linenum > 99.

And the line if (sscanf(line, "%s %s", option, arg)! = 2){ will overrun the option and arg variables if the input strings are too long. (The error you're seeing occurs because the text (e.g. Option1) is longer than 4 characters and overflows option.

You can fix this in several ways: Increase the size of option. You'll need to know the maximum length of the input strings in order to choose an appropriate size. Limit the number of characters that are copied into option: if (sscanf(line, "%3s %99s", option, arg)!

= 2){ This may cause unexpected output for long input strings, because arg will contain the remainder of the string in option if it's truncated. According to linux.die.net/man/3/sscanf you can use the a modifier to tell sscanf to allocate memory for the strings as it reads them: char **str1, **str2; if (sscanf(line, "%as %as", &str1, &str2)! = 2){.

Thanks a lot I really appreciate you took the time to check and explain me the errors in the code, very well explained btw. – gmol Feb 21 at 22:20 You're very welcome. Glad I could help.

– Adam Liss Feb 22 at 1:00.

Your option array is too small (char option4) so sscanf overwrites other stuff. Undefined behavior. Generally when weird things happen for no reason (like things "working" until you declare an unrelated variable) it's because of a memory issue.

Since you mention gcc you might want to look into valgrind.

Wow Thanks a lot, I feel really dumb right now I've been fighting with this for hours, my understanding of how c works with memory it's kinda poor, I'm checking valgrind right now. Btw do you know a way to prevent sscanf from overwriting other things? – gmol Feb 21 at 21:27 @gmol Use safe format specifiers.

Increase the size of option. For example char option20 and then in sscanf use %19s instead of %s. – cnicutar Feb 21 at 21:34 Ok, that's what I'm gona do.

Really appreciate your help thanks. – gmol Feb 21 at 22:13.

Looks like this part at least: if (linelinenum == '#') continue; linenum++; won't give the desired results since you're checking for a comment character at the beginning of the line.

If (sscanf(line, "%s %s", option, arg)! = 2) This overflows option array and will overwrite linenum object.

Thanks a lot this worked :D – gmol Feb 21 at 21:28.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions