1

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.

Polynomial
  • 27,674
  • 12
  • 80
  • 107
cventu
  • 255
  • 1
  • 5
  • 13
  • Possible duplicate of [Reading values from CSV file into variables](http://stackoverflow.com/questions/18737117/reading-values-from-csv-file-into-variables). In particular, see Unwind's answer. – jww Sep 22 '14 at 00:09
  • Yes sorry, I checked that post but used the answer of ppeterka (which is incorrect) and ignored the answer of unwind (which is correct) thinking both of them were the same. Thanks! – cventu Sep 22 '14 at 00:19

1 Answers1

1

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

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

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

Be Kind To New Users
  • 9,672
  • 13
  • 78
  • 125