Reading CSV File Using C Program
June 2, 2008
Since I'm on an integration project, I'm mostly dealing with transferring of data from two or more systems whether it's from the legacy system or newly implemented system. Most of the type of data movement is sending CSV files to and from the different system. Here is a simple tutorial on how to read CSV. The CSV file contains the following:
-
1111,1414,1000
-
1112,1010, 1001
-
1113,1112,1002
The fields are Item Number, Class Number and Supplier Number.
The example below is the simple code of reading CSV file in C.
-
#include <stdio.h>
-
#include <stdlib.h>
-
#include <string.h>
-
-
int main(int argc, char* argv[])
-
{
-
if (argc <2)
-
{
-
fprintf(stderr,"Usage: %s csv_file\n",argv[0]);
-
return(1);
-
}
-
-
FILE *f = fopen(argv[1], "rt");
-
char Line[256];
-
unsigned int AllocSize = 0, Size = 0, n;
-
char *L_text;
-
-
while(fgets(Line, sizeof(Line), f))
-
{
-
-
printf("Item = %s \n",strtok(Line, ", "));
-
printf("Class = %s \n",strtok(NULL, ", "));
-
printf("Supplier = %s \n",strtok(NULL, ", "));
-
-
}
-
return(0);
-
}
Validating Numeric and Non-Numeric Values
June 2, 2008
To check if numeric and non-numeric values, the following syntax can be use.
Syntax
-
select DECODE( TRANSLATE(<value here>,'0123456789',' '), NULL, 'Numeric','Character')
-
from dual;
Example
-
select DECODE( TRANSLATE('12345Z','0123456789',' '), NULL, 'Numeric','Character')
-
from dual;
The example above will return 'Character' because the value has non-numeric characters.





Recent Comments