文件输入/输出用C格式化
问题描述:
typedef char string20[21];
struct x{
string20 a;
string20 b;
string20 c;
};
如何扫描文本文件和它们的值存储在我的结构?我想不出如何做到这一点,顺便说一句,我刚学我一个简单的方法/ O无法找到互联网上的任何好的教程,请帮助
文件格式为:
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
如何在一个存储随机等......我知道我应该使用的strcpy ofcourse
how do i store "random" in a and etc... I know I should use strcpy ofcourse
答
使用与fgets
函数一行。
eg: fgets(buf, MAX_LINE_SIZE, my_io);
使用和strchr
或 strtok的
来找到确切的数据。
Use strchr
or strtok
to find exact data.
eg: ptr = strchr(buf, '=');
复制到你的结构
eg: strcpy(my_structy.ele, ptr);
PS:不要忘了验证。请参考手册页
PS: don't forget validations. refer man pages