0

I'm working on a C function for writing into a CSV file. The problem I am running into is that some of my entries require a comma in the text, which is seperating the test into multiple cells.

For example:

FILE *fp;
fp = fopen(filename,'w+');
fprintf(fp, "red, yellow, blue");

The desired in this case would be cells of "red,yellow","blue" instead of "red" , "yellow", "blue". I've seen others mention there needs to be use of double quotes, but it does't seem to work with fprintf.Any idea how to get this working?

jdhabez
  • 41
  • 4
  • https://stackoverflow.com/questions/11772291/how-can-i-print-a-quotation-mark-in-c – Mat Apr 17 '20 at 14:56
  • 3
    `fprintf(fp, " \"red, yellow\", \"blue\" ");` is what you want? Also: `w+` would truncate the file. If you want to append, `a+` might be more appropriate. – P.P Apr 17 '20 at 14:57
  • try this . fprintf(fp, "%s, %s, %s", val1, val2, val3); Now if you have " ' " in some values then you can use escape sequence for them like strcpy(val1, "\'SomeValue\'");Same goes for double quotes. – D.Malim Apr 17 '20 at 14:58
  • The whole point of csv is (or, rather *was*) that no entries can contain the delimiter. If the delimiter is to appear in an entry, pick another delimiter. All of the efforts over the past decades to introduce methods for escaping delimiters in a csv have just been an unmitigated disaster. Don't use them. If you want commas in an entry, pick a different delimiter. – William Pursell Apr 17 '20 at 14:58
  • Also `fopen(filename,'w+');` -> `fopen(filename, "w+");` – Jabberwocky Apr 17 '20 at 15:06

0 Answers0