Thread: read in csv file

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    7

    read in csv file

    hi

    im having a bit of a problem with a task i have.

    i have 2 csv files in which a list of countries and data is stored, ive managed to read the first csv into an array without a problem, the problem comes when i come to read in the second csv

    what i want to do is run thru the second csv file and read in a figure from it for the corresponding countrty already stored in an array, however the country names are not all the same in the 2 files.

    therefore what ive decided to do is do a strcmp on the country name and only add the data where the country and year matches, this is an example of the code i have so far

    Code:
    typedef struct {
    	int year; // year for which data exists
    	double pop; // Population that year (in millions)
    	double bot;
    	double botPerHead;
    } Data;
    
    typedef struct {
    	char countryName[50]; // maximum length of name
    	char countryCode[4]; // UN assigned 3 character country code
    	Data yearlyData[11]; // max. 12 year’s data (1995 – 2006)
    } Country;
    
    
    int main(void)
    {
    
    	FILE *fppop;
    	FILE *fpbot;
    	Country countries[250];
    	int i = -1;
    	int dataYear;
    	char input[1500];
    	char *p;
    	char previousName[50] = "";
    
    	// read in pop file
    
    	fppop = fopen("un-country-pop.csv", "r");
    
    	if (fppop == NULL) {
    		puts("Error opening file");
    		exit(EXIT_FAILURE);
    	}
    
    	while (!feof(fppop)) {
    
    		fgets(input, 1500, fppop);
    
    		if (input[0] == '#')
    			continue;
    
    		p = strtok (input,",");
    
    		if (strcmp(p, previousName) != 0) {
    			// first time we've seen this country
    			i++;
    			// copy country name to countries[i].
    			strcpy(previousName, p);
    		}
    
    		strcpy(countries[i].countryName, p);
    		p = strtok (NULL,",");
    		strcpy(countries[i].countryCode, p);
    		strtok(NULL,",");
    		strtok(NULL,",");
    		strtok(NULL,",");
    		p = strtok(NULL,","); // year string
    		dataYear = atoi(p);
    		countries[i].yearlyData[2006 - dataYear].year = atoi(p);
    		p = strtok(NULL,","); //pop
    		countries[i].yearlyData[2006 - dataYear].pop = atof(p);
    	}
    
    // read in bot file
    	fpbot = fopen("un-trade-balance.csv", "r");
    	if (fpbot == NULL) {
    		puts("Error opening file");
    		exit(EXIT_FAILURE);
    	}
    
                    i = 0;
    
    	while (!feof(fpbot)) {
    
    		fgets(input, 1500, fpbot);
    
    		if (input[0] == '#')
    			continue;
    
    		p = strtok (input,",");
    
    
    			if (strcmp(p, countries[i].countryName) != 0) {
    			i++;
    			}
    			strtok(NULL,",");
    			strtok(NULL,",");
    			strtok(NULL,",");
    			strtok(NULL,",");
    			p = strtok(NULL,","); // year
    			dataYear = atoi(p);
    			p = strtok(NULL,","); // bop
    			printf("%d\n",i);
    			countries[i].yearlyData[2006 - dataYear].bot = atof(p);
    	}
    }

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Consult the FAQ regarding checking for EOF, foef() isn't the way to do it
    Don't write the same thing twice, put it into a function..
    Also you must fclose() the files, and int main() must return a value to the invoker, eg return 0;

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Also, use some functions

    Before you get to reading from a file, you can do this
    Code:
    void processLine ( char *line );
    int main ( ) {
        char input[1500] = "this,is,a,test\n";
        processLine( input );
    }
    void processLine ( char *line ) {
        // now do all the strtok stuff here
    }
    When you know you can deal with one line successfully, then you can move on to
    Code:
    void processLine ( char *line );
    int main ( ) {
        char input[1500];
        while ( fgets(input, sizeof input, fppop) != NULL ) {
            processLine( input );
        }
    }
    void processLine ( char *line ) {
        // now do all the strtok stuff here
    }
    Notice how easy it is to slide from dealing with one line in memory to reading a whole bunch of lines from a file.
    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.

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Still making this typo?
    Code:
    typedef struct {
        char countryName[50]; // maximum length of name
        char countryCode[4]; // UN assigned 3 character country code
        Data yearlyData[11]; // max. 12 year’s data (1995 – 2006)
    } Country;
    Do you mean Data yearlyData[12];?
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Quote Originally Posted by hk_mp5kpdw View Post
    Still making this typo?
    Code:
    typedef struct {
        char countryName[50]; // maximum length of name
        char countryCode[4]; // UN assigned 3 character country code
        Data yearlyData[11]; // max. 12 year’s data (1995 – 2006)
    } Country;
    Do you mean Data yearlyData[12];?
    But then 1995 - 2006 = 11

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by zacs7 View Post
    But then 1995 - 2006 = 11
    1995 through 2006 inclusive is 12 years.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File Writing Problem
    By polskash in forum C Programming
    Replies: 3
    Last Post: 02-13-2009, 10:47 AM
  2. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  3. c script help to read a file & update
    By indy in forum C Programming
    Replies: 8
    Last Post: 12-01-2003, 11:32 AM
  4. how to read csv file ?
    By kosong in forum C Programming
    Replies: 5
    Last Post: 11-13-2003, 08:14 PM
  5. How to convert char read from file into string
    By cruxxe in forum C Programming
    Replies: 7
    Last Post: 05-22-2002, 02:09 PM