I am trying read CSV files in a C program on an AIX system. I am having problems getting fscanf to recognize the comma delimeter. I have read previous posts on this issue, and used the techniques dicussed in those threads, but I am still having the issue. Specifically, this problem was in a thread from a year ago, and the solution was to remove the comma using %c on integer/decimal fields, and adding a mask for string fields, i.e.,
%[^ \t\n\r\v\f,]%c. I have tried that, but I still am not getting the fscanf to work. It reads all of the line into the first variable, a string variable. I don't know if the problem is that I am running on AIX, and the solution I found is for Linux, or if the problem is that my input CSV file does not have any whitespace, all of the fields are separated only by commas. Anyone have any ideas on what the issue is?
JohnMcPherson
12
Junior Poster in Training
Recommended Answers
Jump to PostPlease post a small program that exhibits the problem and a sample of your CSV file. scanf() format strings can be tricky, and scanf() is usually unsuitable for true CSV, but if it's just a naive comma separated format without embedded commas, you can get away with using scanf().
Jump to PostYou're using the %s specifier, which is whitespace delimited. It won't stop reading when it sees a comma. Try this:
#include <stdio.h> int main(void) { char buf[BUFSIZ]; while (scanf("%[^,\n]%*c", buf) == 1) printf("'%s'\n", buf); return 0; }
All 5 Replies
deceptikon
1,790
Code Sniper
Team Colleague
Featured Poster
JohnMcPherson
12
Junior Poster in Training
deceptikon
1,790
Code Sniper
Team Colleague
Featured Poster
JohnMcPherson
12
Junior Poster in Training
JohnMcPherson
12
Junior Poster in Training
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.