Quantcast
Channel: Active questions tagged gcc - Stack Overflow
Viewing all articles
Browse latest Browse all 22029

Stack smashing detected with fgets() function in C [closed]

$
0
0

I got this code:

while(!feof(fp))
{
   fgets(line,20,fp);
   puts(line);
}

fp is this file:

prova1
prova2
prova3prova4
prova5

The fact is: if the count parameter of the fgets function it's a lower number than 10 it works everything ok, but if it's greater it gives me a stack smashing detected error.

I think it's because I'm trying to read after EOF, but why when I'm trying to read just after it works (like 8 characters per line) but when I put a count parameter of like 20 it gives me an error?

NB: the last line "prova5" is formed by 6 characters plus the '\0' I think, so 7. Why with 8 it's ok and it's not with 20?

EDIT: I'm sorry, I forgot to include this declaration:

char line[] = "";

Viewing all articles
Browse latest Browse all 22029

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>