i do this and it segmentation faults me! why?Code:FILE *g=fopen("out.txt","wt"); fclose(g);
i can printf in the file just fine, but when i try to close it it gives me segmentation fault.. what could the problem be?
i do this and it segmentation faults me! why?Code:FILE *g=fopen("out.txt","wt"); fclose(g);
i can printf in the file just fine, but when i try to close it it gives me segmentation fault.. what could the problem be?
Did you check to see if the fopen() call returned NULL or a valid pointer? fclose()ing NULL could be bad.
It probably fails because of the "wt", I don't know what that "t" is doing there.
t stands for text.
and the pointer is not NULL
Hi All:
Maybe you can alter you code int two line
FILE *g;
if ( ( g=fopen("out.txt","w")) != -1)
fclose(g)
I have not tried using declare a FILE pointer and point it out a file at the same time,
at first. to know a FILE pointer is NULL or not, that is important.I feeling
Thanks ALL
> but when i try to close it it gives me segmentation fault.. what could the problem be?
Maybe it calls malloc, and so do you, and unfortunately you've trashed the memory pool with some buffer overrun.
Unless you can post a whole program which faults, then there isn't much we can do but make wild guesses.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
there was a free() problem. Any advice of links on this issue ? what did i do wrong when i used free and malloc ?
> Any advice of links on this issue ?
Only that you need to be more careful with the way you use memory.
The fact that you used malloc doesn't make a difference, if you step off the end then bad things are going to happen. All that really changes when you do is how your program fails, not if/when your program fails.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.