1

I have unfamiliar issues of fscanf delimiter on C.

The data.txt is below.

k:01062003200:0222005000:G5
g:01034567800::G7
n:01068003200:0222000000:G6
m:01023450987::

As you can see, it is not always that data are written between delimiters ':' but I have to load it anyway even it's "".

fscanf(fileName, "%[^:]:%[^:]:%[^:]:%s", name, phone1, phone2, memo)

This is my code only worked well with a line that no blank between delimiters.

How can I load them all?

Weather Vane
  • 33,872
  • 7
  • 36
  • 56
FlutteRave
  • 11
  • 1
  • 1
    It (`strsep`) is non-standard, but `strtok` cannot handle "empty" fields. – Weather Vane May 13 '20 at 20:14
  • Did it work as well as you thought? In a loop, I would expect it to capture a newline at the start of line 2 onward, because format `%[]` does not filter the newline left by the previous final `%s`. Add a space before the first one, as `" %[^:] ..."` to filter that newline. Please see [scanf() leaves the newline char in the buffer](https://stackoverflow.com/questions/5240789/scanf-leaves-the-new-line-char-in-the-buffer). P.S. always check the return value from `scanf` function family. – Weather Vane May 13 '20 at 20:23
  • ... are you sure that was your exact code? I would not expect to see `fscanf(fileName, ...)` but a variable named more like a file pointer than its name. So I wonder if there was anything else different. You have not yet read the [tour](https://stackoverflow.com/tour) or how to post the [Minimal Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example), the shortest **complete** code that shows the problem. – Weather Vane May 13 '20 at 20:31
  • `fgets` and `strsep` worked well ! Thanks ! :) – FlutteRave May 13 '20 at 23:11

0 Answers0