I have two different problems but are part of one problem: 1. I am using strtok to format a CSV file. The problem tha ti am facing is that some of the values in the CSV file are empty... i.e. the CSV file looks something like: Valeu1,Value2,Value3,,Value4,Value5,,,,Value6 I am using strtok to convert the string into tokens. The problem that i am facing is that it doesn't recognize empty values and take it as the next value... i need a way to make strtok recognize these values. 2. I need to convert dotted IP values present in the CSV file into its equivalent HEX and then into a Int and visa versa.. my code is appended below: Code: #include<stdio.h> #include<conio.h> #include<string.h> #define bufsize 4096 /*A defined integer for our buffer size*/ int main() { /*Program entry point*/ FILE* fp; /*Declare file pointer variable*/ FILE* wr; char *buf[bufsize], *tok, fname[25], fname1[25]; char *q=",", *c="'", *nl="\n"; //char *qq="" int cnt=0; clrscr(); printf("Enter destination filename (with path & extension): "); scanf("%s",&fname1); if((wr=fopen(fname1,"w+"))==NULL) { fprintf(stderr,"Error creating file: %s\n",fname1); getch(); return 1; } printf("Enter Source filename (with path & extension): "); scanf("%s",&fname); /*If file doesn't exist or filetype isn't allowed exit and*/ /*error message & return (1) control to the OS*/ if((fp=fopen(fname,"r")) == NULL) { fprintf(stderr,"Error opening file: %s\n",fname); getch(); return 1; } do { /*Read into the buffer contents within the file stream*/ while(fgets(buf,sizeof(buf),fp)!=NULL) { /*Renew the counter value*/ cnt=1; /*Get the first value in the line*/ tok=strtok(buf,","); /*Write the value present in tok to the file*/ fwrite(tok,strlen(tok),1,wr); /*Insert comma after token*/ fwrite(q,strlen(q),1,wr); /*Here we tokenize our string and scan for "," characters*/ for(tok=strtok(NULL,",");tok;tok=strtok(0,",")) { cnt++; if(cnt==2) { //cnt++; //Call IP convert function1 (dotted IP to Int) fwrite(tok,strlen(tok),1,wr); fwrite(q,strlen(q),1,wr); } if(cnt==4) { //cnt++; //Call IP Convert Function2 (Int to dotted IP) fwrite(tok,strlen(tok),1,wr); fwrite(q,strlen(q),1,wr); } if(cnt==7) { //cnt++; fwrite(c,strlen(c),1,wr); fwrite(tok,strlen(tok),1,wr); fwrite(c,strlen(c),1,wr); fwrite(q,strlen(q),1,wr); } if(cnt==10) { //cnt++; fwrite(c,strlen(c),1,wr); fwrite(tok,strlen(tok),1,wr); fwrite(c,strlen(c),1,wr); fwrite(q,strlen(q),1,wr); } if(cnt==11) { //cnt++; fwrite(c,strlen(c),1,wr); fwrite(tok,strlen(tok),1,wr); fwrite(c,strlen(c),1,wr); fwrite(q,strlen(q),1,wr); } if(cnt==17) { //cnt++; fwrite(c,strlen(c),1,wr); fwrite(tok,strlen(tok),1,wr); fwrite(c,strlen(c),1,wr); fwrite(q,strlen(q),1,wr); } else { if(cnt==22) { fwrite(tok,strlen(tok),1,wr); fwrite(nl,strlen(nl),1,wr); } if(cnt==2||cnt==4||cnt==7||cnt==8||cnt==10||cnt==11||cnt==13||cnt==14||cnt==16||cnt==17||cnt==22||cnt==23) { //cnt++; } else { fwrite(tok,strlen(tok),1,wr); fwrite(q,strlen(q),1,wr); } } } } }while(fp==NULL); fclose(fp); /*Close file*/ fclose(wr); printf("\nThe output has been written to the file %s",fname1); getch(); return 0; /*Executed without errors*/ }/*End main*/
My first suggestion would be to add some functions to separate some of the actions out from main. This will reduce clutter, and make testing individual parts much easier. Eg. Code: while(fgets(buf,sizeof(buf),fp)!=NULL) { parseLine(buff,fields); // assign each CSV to the fields array processFields(fields); } Then replace strtok() with http://www.hmug.org/man/3/strsep.php if you have such a function.