Thread: Struct Population Problems in C

  1. #1
    Registered User
    Join Date
    Feb 2017
    Posts
    1

    Struct Population Problems in C

    I am trying to sift through a .csv file and populate a struct at the top of the code in order to spit out the fields into a terribly made table that I slapped together. But whenever I try to compile it, it prints up to 2 fields fine before printing (null). What do? Please help me, I'm literally dying.


    http://i.imgur.com/1eK5uqW.png


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <strings.h>
    #include <ctype.h>
    
    
    /* Remember to turn struct into .h */
    typedef struct airPdata{
     char *siteNumber; //FAA Site Number
     char *LocalID; //Airport's "Short Name", ie MCO
     char *fieldName; //Airport Name
     char *city; //Associated City
     char *state; //State
     char *latitude; //Latitude
     char *longitude; //Longitude
     char *controlTower; //Control Tower (Y/N)
    }airPdata;
    
    
    /* Sifts through .csv file and splits strings up into tokens by , delimiter. Returns the token to print function. */
    char *getfield(char *line, int num){
    	char *tok;
    	for (tok = strtok(line, ","); tok && *tok; tok = strtok(NULL, ",\n"))
    	{
    		if(!--num)
    			return tok;
    	}
    	return NULL;
    }
    
    
    /* Print function that spits out tokens sorted by getfield function. */
    void print(char *file, FILE *fp, char *line)
    	{	
    	airPdata *ports;
    	ports = (airPdata *) malloc(sizeof(airPdata));
    	ports->siteNumber = malloc(sizeof(char)*1000);
    	ports->fieldName = malloc(sizeof(char)*1000);
    	ports->LocalID = malloc(sizeof(char)*1000);
    
    
    		char *tmp=strdup(line);
    		while(fgets(line, 500, fp))
    		{
    			char *tmp=strdup(line);
    			ports->LocalID = getfield(tmp,2);
    			ports->siteNumber = getfield(tmp, 1);
    			ports->fieldName = getfield(tmp, 3);
    			printf("%s	%s	%s\n", ports->siteNumber, ports->LocalID, ports->fieldName);
    			free(tmp);
    		}
    	}
    	
    int main()
    {
    	//airPdata *ports;
    	char file[20];
    	char line[1000];
    	int col;
    	int row;
    	
    	printf ("Input Desired File Name: ");
    	scanf ("%s", &file);
    	FILE *fp = fopen(file, "r");
    	
    	if(fp==NULL)
    	{
    		printf("\nNo file...\n");
    		return -1;
    	}
    	
    			printf("\nFAA Site#	Short Name	Airport Name	");
    			printf("City	ST	Latitude	");
    			printf("Longitude	Tower\n");
    			printf("----------	----------	----------	");
    			printf("----	--	--------	");
    			printf("---------	-----\n");
    
    
    	print(file, fp, line);
    	fclose(fp);
    
    
    	return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    The main problem is you're trying to strtok() the same string multiple times.

    > ports->LocalID = getfield(tmp,2);
    > ports->siteNumber = getfield(tmp, 1);
    > ports->fieldName = getfield(tmp, 3);
    The problem is at the 2nd call, all the strtok calls have completely trashed your tmp string.

    Examine this
    Code:
                char *tmp=strdup(line);
                printf("Get 2nd field from string %s\n", tmp);
                ports->LocalID = getfield(tmp,2);
                printf("Get 1st field from string %s\n", tmp);
                ports->siteNumber = getfield(tmp, 1);
                printf("Get 3rd field from string %s\n", tmp);
                ports->fieldName = getfield(tmp, 3);
    > The image “http://i.imgur.com/1eK5uqW.png” cannot be displayed, because it contains errors.


    FWIW, start simple.
    Code:
    typedef struct airPdata{
     char siteNumber[100]; //FAA Site Number
     char LocalID[100]; //Airport's "Short Name", ie MCO
     char fieldName[100]; //Airport Name
     char city[100]; //Associated City
     char state[100]; //State
     char latitude[100]; //Latitude
     char longitude[100]; //Longitude
     char controlTower[100]; //Control Tower (Y/N)
    }airPdata;
    You can choose more reasonable numbers later, or use malloc/strdup with pointers, once everything else is working.

    Try
    Code:
    char line[BUFSIZ];
    airPdata port;
    while ( fgets(line,sizeof(line),fp) != NULL ) {
      if ( sscanf(line,"%[^,],%[^,],%[^,],",
            port.siteNumber, port.LocalID, port.fieldName) == 3 ) {
        printf("success\n");
      }
    }
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 11-16-2011, 08:42 AM
  2. 2 problems: Struct array and pointer + struct
    By v1n1c1u5 in forum C Programming
    Replies: 0
    Last Post: 12-13-2009, 05:38 PM
  3. Sort by population or area
    By the_lumin8or in forum C++ Programming
    Replies: 3
    Last Post: 09-30-2006, 07:12 AM
  4. struct problems
    By Shotgun in forum C Programming
    Replies: 3
    Last Post: 06-01-2006, 02:21 AM

Tags for this Thread