Thread: SegFault when reading a file

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    8

    SegFault when reading a file

    I keep getting a seg fault at the end of a file read. Each line of the file is around 500+ characters so MAX_LEN is 600. Any ideas?
    Code:
    //~ _________________________________________________
    //~ Read GIS data
    void read_gis(char *gis,GooCanvasItem *canvas)
    {	
    	FILE * gis_file;
    	//~ gchar *path_data;
    	char path[MAX_LEN + 1];
    	int count = 0;
    	size_t i;
        
    	gis_file = fopen(gis,"r");
    	if (gis_file != NULL)
    	{
    		while ( fgets(path, MAX_LEN+1, gis_file) != NULL )
    		{
    			count++;
    			printf("%d %s\n\n",count,path);
    			i = strlen(path);
    			printf("strlen %d\n",i);
    			path[i+1] = '\0';
    			
    		
    			//~ path_data = path;
    			//~ goo_canvas_path_new(canvas,path_data,"stroke-color","white","line-width",5.0,NULL);
    			//~ goo_canvas_path_new(canvas,"M 20,20 L 4000,4000","stroke-color","white","line-width",5.0,NULL);
    		}
    	}
    	fclose (gis_file);
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    So how do you know this function is to blame?

    > i = strlen(path);
    > path[i+1] = '\0';
    What exactly are you trying to do here?

    p[ strlen(p) ] indexes the \0 character at the end of a string.
    If fgets() FILLED the buffer completely, there is still a \0 at the end (the very end), and all you managed to do here was trash someone else's memory.

    Oh, and your fclose() call should be inside the if NULL test
    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.

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    8
    You're right. I does not occur in this subroutine. It actually occurs in the one below, right below the printf statement. It seems the fscanf is causing it. Any ideas?
    Code:
    //~ _________________________________________________
    //~ Read RADAR data
    
    void read_radar(char *radar,GooCanvasItem *group)
    
    {	
    	FILE * radar_file;
    
    	//~ Open File
    	if ((radar_file = fopen(radar,"r")) != NULL)
    	{
    		char *call;
    		float lat;
    		float lon;
    		char *name;
    
    		while(!feof(radar_file))
    		{
    			printf("HERE3\n");
    			fscanf(radar_file,"%s %f %f %s",call,&lat,&lon,name);
    			printf("%s %f %f %s\n",call,lat,lon,name);
    
    			lon = (top_long - lon) / -ps;
    			lat = (top_lat - lat) / ps;
    
    			printf("%s %f %f %s\n",call,lat,lon,name);
    			goo_canvas_ellipse_new(group,lon,lat,5.0,5.0,"fill-color","yellow","stroke-color","black",NULL);
                   goo_canvas_text_new(group,name,lat,lon,-1.0,GTK_ANCHOR_CENTER,"font","Sans Bold 15","fill-color","black",NULL);
    		}
    		fclose (radar_file);
    	}
    	else
    	{
    		printf("Radar File Not Opened\n");
    		exit(1);
    	}
    }

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    You need to allocate memory for name and call before you can read data into them. All you have there are pointers to Dog knows where.

  5. #5
    Registered User
    Join Date
    May 2009
    Location
    Slovenia
    Posts
    5
    Hi.

    fscanf fails because call and name pointers point to "only-god-knows-where". Try replacing them with character arrays instead like this:
    Code:
    		char call[512];
    		float lat;
    		float lon;
    		char name[512];
    
    		while(!feof(radar_file))

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    8
    That worked! And finally goo is displaying things. But I have suddenly started to get seg faults again. this time I cant really pin point the source but I think it is the while causing it since all of my printf's show up except the 4th one.
    Code:
    //~ _________________________________________________
    //~ Read RADAR data
    void read_radar(char *radar,GooCanvasItem *group)
    {	
    	FILE * radar_file;
    	
    	//~ Open File
    	if ((radar_file = fopen(radar,"r")) != NULL)
    	{
    		char call[4];
    		float lat;
    		float lon;
    		char name[25];
    
    		while(!feof(radar_file))
    		{
    			fscanf(radar_file,"%s %f %f %s",call,&lat,&lon,name);
    			printf("%s %f %f %s\n",call,lat,lon,name);
    			
    			lon = (top_long - lon) / -ps;
    			lat = (top_lat - lat) / ps;
    			
    			printf("%s %f %f %s\n",call,lat,lon,name);
    			
    			printf("HERE1\n");
    			
    			goo_canvas_ellipse_new(group,lon,lat,5.0,5.0,"fill-color","yellow","stroke-color","black",NULL);
    			printf("HERE2\n");
    			goo_canvas_text_new(group,name,lat,lon,-1.0,GTK_ANCHOR_CENTER,"fill-color","green",NULL);
    			printf("HERE3\n");
    		}
    		printf("HERE4\n");
    		fclose (radar_file);
    	}
    	else
    	{
    		printf("Radar File Not Opened\n");
    		exit(1);
    	}
    }

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > void read_gis(char *gis,GooCanvasItem *canvas)

    Then you post
    > void read_radar(char *radar,GooCanvasItem *group)

    > You're right. I does not occur in this subroutine. It actually occurs in the one below
    Thanks

    But there are more errors in this function, besides those already mentioned. And they're answered in the FAQ.
    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.

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by deadpickle View Post
    But I have suddenly started to get seg faults again.
    A little tip: when using printf for debugging like this, place a fflush() call afterwards:
    Code:
    printf("HERE2"); fflush(stdout);
    because with a segfault, execution may have already passed this point but not yet flushed output to the console (and it will never happen after the fault), so you can get some misleading results. But with the fflush(), the printf statement will appear immediately.

    You might also want to try a real debugger like "gdb" for this. It is one thing they are REALLY good at -- it will give you the exact line that caused the fault 80% of the time.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  9. #9
    Registered User
    Join Date
    May 2009
    Posts
    8
    Finally got GDB to work after installing a train of debugs. It is as I expected:
    0xb75d70a1 in _IO_feof (fp=0x88b6600) at feof.c:37
    37 _IO_flockfile (fp);

    The error seems to be in the while loop. Any ideas on how to fix it?

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Stop using feof to control your loop.


    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    At the segfault, you then use the 'bt' command to tell you all the nested functions which have been called to reach that point.

    You then look at the innermost one of that list which is YOUR code, and start looking really hard at all the parameters you pass to whatever library function you happen to be calling.

    It might not be the answer, but it's a good (and necessary) place to start looking.
    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.

  12. #12
    Registered User
    Join Date
    May 2009
    Posts
    8
    Thank you for all of your help. Its good to learn how to debug a program.

    From what this sounds like to me is that there is an error in reading the radars.gis file at the end. The line it points to looks like such:
    Code:
    while(!feof(radar_file))
    #1 0x080492fd in read_radar (radar=0x8049b68 "/mnt/share/uas/GRRUVI2_uc/radars.gis",
    group=0x88aeae8) at goo_0.011.c:68
    #2 0x080498f0 in main (argc=Cannot access memory at address 0x6
    ) at goo_0.011.c:193

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Remember when I told you to stop using feof to control your loop?


    Quzah.
    Hope is the first step on the road to disappointment.

  14. #14
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Post read_radar() again, and put a comment next to line 68.
    You did look at line 68 to try and figure out why it would barf?
    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.

  15. #15
    Registered User
    Join Date
    May 2009
    Posts
    8
    Code:
    //~ _________________________________________________
    //~ Read RADAR data
    void read_radar(char *radar,GooCanvasItem *group)
    {	
    	FILE * radar_file;
    
    	//~ Open File
    	if ((radar_file = fopen(radar,"r")) != NULL)
    	{
    		char call[4];
    		float lat;
    		float lon;
    		char name[25];
    
    		while(!feof(radar_file))    //~ the line causing the pain
    		{
    			fscanf(radar_file,"%s %f %f %s",call,&lat,&lon,name);
    			printf("%s %f %f %s\n",call,lat,lon,name);
    			
    			lon = (top_long - lon) / -ps;
    			lat = (top_lat - lat) / ps;
    			
    			printf("%s %f %f %s\n",call,lat,lon,name);
    			
    			goo_canvas_ellipse_new(group,lon,lat,5.0,5.0,"fill-color","yellow","stroke-color","black",NULL);
    			goo_canvas_text_new(group,name,lat,lon,-1.0,GTK_ANCHOR_CENTER,"fill-color","green",NULL);
    		}
    		fclose (radar_file);
    	}
    	else
    	{
    		printf("Radar File Not Opened\n");
    		exit(1);
    	}
    }
    THanks for all the help.

    If the way I do the while loop is wrong, then what are my options for looping each line of a file and reading its contents using fscanf?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  2. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  3. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM