Reading in a two line file

This is a discussion on Reading in a two line file within the C Programming forums, part of the General Programming Boards category; I am trying to read a file with this subroutine. It has two lines and looks as such: kpaw 40.871 ...

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

    Reading in a two line file

    I am trying to read a file with this subroutine. It has two lines and looks as such:
    kpaw 40.871 -104.714 Pawnee Radar

    kchl 40.446 -104.637 CHILL Radar


    When I run the program I get this readout in the terminal:
    kchl 40.446 -104.637 CHILL Radar14 Pawnee Radar
    radar string: �1m�x1m��1m��1m�▒�

    Not sure whats going on. Any ideas?

    Code:
    void read_radar(char *radar,GooCanvasItem *group)
    
    {	
    
    	FILE * radar_file;
    
    	//~ gchar *path_data;
    
    	
    
    	radar_file = fopen(radar,"r");
    
    	if (radar_file != NULL)
    
    	{
    
    		char radar_string[80];
    
    		const char delimiters[] = " ";
    
    		char *call;
    
    		char *chlat;
    
    		char *chlon;
    
    		char *name1;
    
    		char *name2;
    
    		double lat;
    
    		double lon;
    
    		
    
    		//~ fgets(radar_string, 40, radar_file);
    
    		while ( fgets(radar_string, 80, radar_file) != NULL )
    
    		{
    
    			printf("radar string: %s\n",radar_string);
    
    			fflush(NULL);
    
    			fclose (radar_file);
    
    		
    
    			call = strtok (radar_string, delimiters); //~ Radar Call
    
    			chlat = strtok (NULL, delimiters); //~ Radar Lat
    
    			chlon = strtok (NULL, delimiters); //~ Radar Long
    
    			name1 = strtok (NULL, delimiters); //~ Radar Name1
    
    			name2 = strtok (NULL, delimiters); //~ Radar Name2
    
    		
    
    			lat = strtod(chlat, (char **) NULL);
    
    			lon = strtod(chlon, (char **) NULL);
    
    		
    
    			//~ printf("%s %f\n",chlat,lat);
    
    			//~ printf("%s %f\n",chlon,lon);
    
    		
    
    			lon = (top_long - lon) / -ps;
    
    			lat = (top_lat - lat) / ps;
    
    		
    
    			//~ printf("%s %f\n",chlat,lat);
    
    			//~ printf("%s %f\n",chlon,lon);
    
    		
    
    			//~ goo_canvas_ellipse_new(group,lon,lat,5.0,5.0,"fill-color","yellow","stroke-color","black",NULL);
    
    			//~ Goo::Canvas::Text->new ($radars,$radar_name,$radar_x,$radar_y,-1,'GTK_ANCHOR_CENTER',font => 'Sans Bold 15',fill_color => 'black');
    
    		}
    
    	}
    
    	else
    
    	{
    
    		printf("ended\n");
    
    	}
    
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Any time you see garbled looking output like that, it means you've forgotten to null terminate a "string".


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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 11:39 PM
  2. Reading random line from a text file
    By helloamuro in forum C Programming
    Replies: 24
    Last Post: 05-03-2008, 11:57 PM
  3. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 05:54 PM
  4. reading file line by line
    By ameet in forum C Programming
    Replies: 7
    Last Post: 09-16-2004, 11:54 AM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21