Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
fclose crashing my app?
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
slougi
Apprentice
Apprentice


Joined: 12 Nov 2002
Posts: 222
Location: Oulu, Finland

PostPosted: Sun Mar 23, 2003 10:52 pm    Post subject: fclose crashing my app? Reply with quote

Hi there,
I am ye typical C newbie so bear with me ;)

Anyway, been having some fun learning C, and have found something (imho) pretty strange. Anyway, here is my code:

Code:
#include <stdio.h>
                                                                                                               
int main()
{
   FILE *xmms;
   char *song;
   if ((xmms = fopen("/home/louai/.xmms_song" , "r")) == NULL)
     {
        printf("Failed while loading file!\n");
        return 1;
     }
   printf("File is read!\n");
   song=(char *) malloc(sizeof(xmms));
   while (fgets(song, 200, xmms) != NULL)
     {
        printf("%s", song);
     }
   fclose(xmms);
   free(song);
   return 0;
}


So I have xmms set up to write the current song title to ~/.xmms_song. This works fine until it reaches fclose(xmms); which causes a segfault. As far as I can see there is nothing wrong. xmms is not a null pointer at this point, and should be closable. Someone want to tell me what is wrong? :cry:
Back to top
View user's profile Send private message
slougi
Apprentice
Apprentice


Joined: 12 Nov 2002
Posts: 222
Location: Oulu, Finland

PostPosted: Mon Mar 24, 2003 8:41 am    Post subject: Reply with quote

Ok this does not happen at school. Does someone want to tell me what is wrong?
Back to top
View user's profile Send private message
()
l33t
l33t


Joined: 25 Nov 2002
Posts: 610

PostPosted: Mon Mar 24, 2003 9:38 am    Post subject: Reply with quote

Hmm... Im really more used to C++ than C, but theres something about this I find slightly confusing. You declare a string the size of one pointer, and you can fill that with an arbitrary number of characters? I actually tried this myself in Visual Studio, and even if I malloc only one byte, I can read in long strings.
Edit: Come to think of it, you're probably just lucky that youre able to write beyond your allocated memory. Other times you're not so lucky and get a SIGSEGV. If you do a sizeof ( FILE *), all you get is the size of a pointer, most of the time 4 bytes. What you need to do I think, is fseek( xmms, 0, SEEK_END ), followed by ftell and rewind.
Back to top
View user's profile Send private message
slougi
Apprentice
Apprentice


Joined: 12 Nov 2002
Posts: 222
Location: Oulu, Finland

PostPosted: Mon Mar 24, 2003 8:46 pm    Post subject: Reply with quote

() wrote:
Hmm... Im really more used to C++ than C, but theres something about this I find slightly confusing. You declare a string the size of one pointer, and you can fill that with an arbitrary number of characters? I actually tried this myself in Visual Studio, and even if I malloc only one byte, I can read in long strings.
Edit: Come to think of it, you're probably just lucky that youre able to write beyond your allocated memory. Other times you're not so lucky and get a SIGSEGV. If you do a sizeof ( FILE *), all you get is the size of a pointer, most of the time 4 bytes. What you need to do I think, is fseek( xmms, 0, SEEK_END ), followed by ftell and rewind.

Thanks a lot! I got it now :)
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum