Skip to content

Instantly share code, notes, and snippets.

@znedw
Created September 30, 2012 11:05
Show Gist options
  • Save znedw/3806468 to your computer and use it in GitHub Desktop.
Save znedw/3806468 to your computer and use it in GitHub Desktop.
csv parser
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
char *headings[40][7] ;
struct food {
char food[100];
char measure[50];
char weight[60];
char kcal[30];
char fat[50];
int carb;
int protein;
} ;
struct food foodlist[4];
int main() {
int i ;
headings[40][0] = "Obs" ;
FILE *fp = fopen("calories.csv", "r") ;
if ( fp != NULL )
{
fscanf(fp, "%s, %s, %s, %s, %s, %i, %i \n",
headings[40][1], headings[40][2], headings[40][3],
headings[40][4], headings[40][5], headings[40][6], headings[40][7]) ;
for(i=0; i<5; i++)
{
fscanf(fp, "%s, %s, %s, %s, %s, %i, %i \n",
foodlist[i].food, foodlist[i].measure, foodlist[i].weight,
&foodlist[i].kcal, foodlist[i].fat, foodlist[i].carb, foodlist[i].protein );
/* i++; */
} /* EOF */
fclose(fp);
} /* File exists */
/* Print the file */
printf("%s, %s, %s, %s, %s, %i, %i \n",
headings[40][1], headings[40][2], headings[40][3],
headings[40][4], headings[40][5], headings[40][6], headings[40][7]);
int j;
for (j=0; j<5; j++)
{
printf("%s, %s, %s, %s, %s, %i, %i \n",
foodlist[i].food, foodlist[i].measure, foodlist[i].weight,
&foodlist[i].kcal, foodlist[i].fat, foodlist[i].carb, foodlist[i].protein);
/* j++; */
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment