Hi,
This is what my code currently does. Reads a CSV file, parses the contents, displays a set number of columns and prints a specific value from a specified column and row at the end. Now ive been getting a lot of help on this code as im still trying to learn C.
This is what i now need the project to. The values in some of the columns need to be extracted and used for the maths equation, which will then print the result at the end of the line. An example is that I would like to take the value from the 4th column along, on each line (which is a date), and then put this in a variable to be called by another part of my equation, in my case something to work out the difference between the two dates.
Code:
#include "stdafx.h"
#include <stdio.h>
int main() {
const int fields_to_show = 4;
const int record_to_show = 1;
const int field_to_show = 8;
char field_buff[256];
int prt_field_buff = 0;
FILE * fp;
int c;
int cnt_of_fields;
int cnt_of_records;
if((fp = fopen("file2.csv","r")) == NULL) {
printf("Can't connect to file\n");
return -2;
}
cnt_of_fields = 0;
cnt_of_records = 0;
while((c = fgetc(fp)) != EOF) {
switch(c)
{
case ',' :
if(cnt_of_records == record_to_show - 1 && cnt_of_fields == field_to_show - 1)
field_buff[prt_field_buff++] = '\0';
cnt_of_fields++;
if(cnt_of_fields < fields_to_show)
printf(" - ");
break;
case '\n' :
if(cnt_of_records == record_to_show - 1 && cnt_of_fields == field_to_show - 1)
field_buff[prt_field_buff++] = '\0';
cnt_of_fields = 0;
cnt_of_records++;
printf("\n");
break;
default :
if(cnt_of_records == record_to_show - 1 && cnt_of_fields == field_to_show - 1)
field_buff[prt_field_buff++] = c;
if(cnt_of_fields < fields_to_show)
printf("%c",c);
} /* switch */
} /* while */
printf("%s\n",field_buff);
fclose(fp);
return 0;
}
Can someone tell me the best way of doing this please? I dont want to know the code i need, but if you can tell me how you would code it then i can look into how to do it.