File input/output in C unformatted

2019-09-01 09:22发布

typedef char string20[21];

struct x{
string20 a;
string20 b;
string20 c;
};

How do I scan a text file and store their values on my structure? I can't think of an easy way on how to do this and btw I'm just learning I/O can't find any good tutorial on internet please help the file format is:

3
FCODE=random
FKEY=shit
FSRC=hi

how do i store "random" in a and etc... I know I should use strcpy ofcourse

1条回答
▲ chillily
2楼-- · 2019-09-01 10:04

Use fgets function to a single line.

 eg: fgets(buf, MAX_LINE_SIZE, my_io);

Use strchr or strtok to find exact data.

 eg: ptr = strchr(buf, '=');

copy into your structure

 eg: strcpy(my_structy.ele, ptr);

PS: don't forget validations. refer man pages

查看更多
登录 后发表回答