问题
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