简体   繁体   中英

Read csv file using fscanf in C

I have to read a csv file using fscanf function (I cant use any other function like strtok_s to parse the line) and im having the following problem.

Here is the code:

fp1 = fopen (argv [1],"r");

var = fscanf (fp1,"%d,%d,%[^,]s,%[^,]s",&aux.points,%aux.titles,aux.name,aux.nation);

I'm trying to print each parameter in the screen. There is no problem with the integers and even with the first string (name) but nothing is stored in the next string (nation).

I assume that the first %[^,]s is stopping the execution of the whole fscanf function so the next string is never read. Any idea? I have tried everything but this is just not working.

Try this as the string: "%d,%d,%[^,],%[^,]"

I eliminated the "s" because [...] acts as the specifier.

Think of the [...] as a super s.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM