fscanf() reading string with spaces in formatted lines

c, scanf

Solution

The main issue is `"%[^\n],"`. The `"%[^\n]"` scans in all except the `'\n'`, so `description` scans in the `','`. Code needs to stop scanning into `description` when a comma is encountered.

With line orientated file data, 1st read 1 line at a time.

char buf[100];
if (fgets(buf, sizeof buf, file) == NULL) Handle_EOForIOError();

Then scan it. Use `%39[^,]` to not scan in `','` and limit width to 39 `char`.

int cnt = sscanf(buf,"%d , %39[^,],%d", &p.code, p.description, &p.price);
if (cnt != 3) Handle_IllFormattedData();

Another nifty trick is: use `" %n"` to record the end of parsing.

int n = 0;
sscanf(buf,"%d , %39[^,],%d %n", &p.code, p.description, &p.price, &n);
if (n == 0 || buf[n]) Handle_IllFormattedData_or_ExtraData();

[Edit]

Simplification: @user3386109

Correction: @cool-guy remove &

Problem

Using this structure: ``` typedef struct sProduct{ int code; char description[40]; int price; }; ``` I want to read a txt file with this format: ``` 1,Vino Malbec,12 ``` where the format is: `code,description,price`. But I'm having problems to read the description when it has a space. I tried this: ``` fscanf(file,"%d,%[^\n],%d\n",&p.code,&p.description,&p.price); ``` The code is being saved ok, but then in description is being saved `Vino Malbec,12`, when I only want to save `Vino Malbec` because `12` is the price. Any help? Thanks!

Original source