File input/output in C unformatted

放肆的年华 提交于 2019-12-25 05:07:19

问题


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:


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



来源:https://stackoverflow.com/questions/22879109/file-input-output-in-c-unformatted

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!