how to escape values for writing a CSV file using C language -


i working on project write data csv file. how escape values writing csv file using c language.

void writetofile(struct radatastructure data) {     file *fp;     fp = fopen("result.data", "a");     fprintf(fp, "%s,", data.long);     fprintf(fp, "%s,", data.lat);     fprintf(fp, "%s,", data.city);     fprintf(fp, "%d,", data.pobox);     fprintf(fp, "%s,", data.bio);     fprintf(fp, "%d,", data.bnumber);     fclose(fp); } 

the filed data.bio may contain character including comma, quote , slashes. how can escape before writing file make valid csv file.

this should give starting place. note need free() value returns when done it.

char* escapecsv(char* in) {   int in_len = strlen(in);   char *out_buf = malloc(in_len*2+3);   int out_idx = 0;   int in_idx = 0;    out_buf[out_idx++] = '"';   for(in_idx=0; in_idx < in_len; in_idx++) {     if(in[in_idx] == '"') {       out_buf[out_idx++] = '"';       out_buf[out_idx++] = '"';     } else {       out_buf[out_idx++] = in[in_idx];     }   }   out_buf[out_idx++] = '"';   out_buf[out_idx++] = 0;   return out_buf; } 

Comments

Popular posts from this blog

c# - How to get the current UAC mode -

postgresql - Lazarus + Postgres: incomplete startup packet -

javascript - Ajax jqXHR.status==0 fix error -