Hi all,
I have huge data files with different line legths and trying to load into tables. I would like to write a generic function which i can read through file. and as i said..it deals with huge quatity of data.
here is what i wrote but am running into problems..when i get Two commas at a time.
My function is as follows :
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
void parseIt()
{
char string[1028];
char *token[120];
char fname[128];
int a,b,i;
gargc = argc;
gargv = argv;
memset(fname,0,sizeof(fname));
sprintf(fname,"%s/%s",gargv[1],gargv[2]);
if((fp1 = fopen(fname, "r"
)==NULL) {
fprintf(stdout,"CAnnot Open File \n"
;
exit(1);
}
while(fgets(string,1027,fp1) != NULL) {
for (i = 0; i< strlen(string) ; i++) {
if(string == '\n') {
string = '\0';
}
}
a=0;
token[a++] = strtok(string,","
;
while(token[a++] = strtok(NULL,","
) ;
a--;
if(debugflag) {
for(b=0;b<a;b++)
printf("%i: \"%s\"\n",b+1,token);
}
}
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
and sample file is
A,123,24,hello
A23,Howrya
Junk,Junk1,,junk2,,junk3
It all goes fine for the first 2 lines but for the third line it parses out sure but i want empty or null in that place..but i get 4 fields instead for 6 fields. a zero for NUll also would do..infact it is cool( if we have a way to do it in function)..All the above is an effort to rewrite a chunk of code which uses "sscanf" and runs into lot of buffer overflow problems.
I believe i have to use strrchr but am not sure..
Any suggestions..in the above chunk
thanks in advance
Logic4fun
I have huge data files with different line legths and trying to load into tables. I would like to write a generic function which i can read through file. and as i said..it deals with huge quatity of data.
here is what i wrote but am running into problems..when i get Two commas at a time.
My function is as follows :
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
void parseIt()
{
char string[1028];
char *token[120];
char fname[128];
int a,b,i;
gargc = argc;
gargv = argv;
memset(fname,0,sizeof(fname));
sprintf(fname,"%s/%s",gargv[1],gargv[2]);
if((fp1 = fopen(fname, "r"
fprintf(stdout,"CAnnot Open File \n"
exit(1);
}
while(fgets(string,1027,fp1) != NULL) {
for (i = 0; i< strlen(string) ; i++) {
if(string == '\n') {
string = '\0';
}
}
a=0;
token[a++] = strtok(string,","
while(token[a++] = strtok(NULL,","
a--;
if(debugflag) {
for(b=0;b<a;b++)
printf("%i: \"%s\"\n",b+1,token);
}
}
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
and sample file is
A,123,24,hello
A23,Howrya
Junk,Junk1,,junk2,,junk3
It all goes fine for the first 2 lines but for the third line it parses out sure but i want empty or null in that place..but i get 4 fields instead for 6 fields. a zero for NUll also would do..infact it is cool( if we have a way to do it in function)..All the above is an effort to rewrite a chunk of code which uses "sscanf" and runs into lot of buffer overflow problems.
I believe i have to use strrchr but am not sure..
Any suggestions..in the above chunk
thanks in advance
Logic4fun