i trying read file in c in way load simple variables , values. example, let's text of file:
a=100 b=hello c=world
ow idea load each line of file such string on left of equals sign name of variable, , string on right loaded in value. code read file in way described:
#include <stdio.h> int main(void) { file* f; f = fopen("test.txt", "r"); char name[256]; char value[256]; while(fscanf(f, "%255[^=]=%255[^=]", name, value) == 2) printf("%s, %s\n", name, value); return 0; }
but when compile , run it, output is:
a, 100 b
i should mention have basic grasp of how delimiters work in scanf
or fscanf
formatting strings. have feeling that's what's going wrong here, have yet find or stumble upon solution. appreciated!
%255[^=]
read =
on first line =
on second line, since didn't include newline delimiter. it's setting name
"100\nb"
. next fscanf()
fails because there's no string before =
.
use newline second delimiter rather =
, , put space before first format skips on whitespace before parsing each line.
while(fscanf(f, " %255[^=]=%255[^\n]", name, value) == 2)
Comments
Post a Comment