g.saltuk
g.saltuk

Reputation: 21

C: Ignoring a specific character, while using fscanf

As an example I have a text file that includes this text: "name?"

I want to save this String only as name?

I tried ("%["]"), but this doesn't work.

Which function should I use?

Upvotes: 1

Views: 739

Answers (1)

Some programmer dude
Some programmer dude

Reputation: 409266

The scanf and fscanf functions work exactly the same. Your format is however wrong.

Try instead e.g. "\"%[^\"]\"" as your format.

The first and last " is to mark the start and end of the string. Inside the string one can't use plain double-quote as that will end the string. So these have to be escaped using the backslash.

If we break down the format string into its three main components:

  • \" - This matches the literal double-quote
  • %[^\"] - This matches a string not containing the double-quote (the negation is what the ^ does)
  • Lastly \" again, to match the end quote of your input

Upvotes: 2

Related Questions