Bernt Smith
Bernt Smith

Reputation: 101

scanf and format related?

I have a file with the following content

"AAA";"BBB"

I try to extract the 2 columns this way :

char v1[50];
char v2[50];
int ret = fscanf(fp, "\"%s\";\"%s\"", v1, v2);

But it returns 1 and everything in 'v1' !

Is it normal ?

Upvotes: 4

Views: 50

Answers (1)

Some programmer dude
Some programmer dude

Reputation: 409442

It's because the "%s" format reads space delimited strings. It will read the input until it hits a white-space or the end of the input.

You could possibly use the "%[" format here, maybe something like

fscanf(fp, "\"%[^\"]\";\"%[^\"]\"", v1, v2);

See e.g. this scanf (and family) reference for more information.

Upvotes: 5

Related Questions