[英]Reading values from file and storing to different struct in C
I have a file of following format 我有以下格式的文件
[KeyString1]
Value1,Value2,Value3
<blankline>
[KeyString2]
Value1,Value2a,Value3a
Now I want read values which belongs to KeyString1 and KeyString2 separately and store in different Structure. 现在,我想分别读取属于KeyString1和KeyString2的值并存储在不同的Structure中。
This is what i have tried till now 这是我到目前为止一直尝试的
enum VAR_TYPE
{
VAR_KeyString1,
VAR_KeyString2,
};
int main(void) {
FILE *fp;
char temp[512];
int t_scanType;
if ((fopen_s(&fp, "/text.txt", "r")) != NULL) {
return(-1);
}
t_scanType = -1;
while(fgets(temp, 512, fp) != NULL)
{
if (temp == " ") {
continue;
}
if ((strstr(temp,"[KeyString1]")) != NULL) {
t_scanType=VAR_KeyString1;
continue;
}
if ((strstr(temp,"[KeyString2]")) != NULL) {
t_scanType=VAR_KeyString2;
continue;
}
switch(t_scanType)
{
case VAR_KeyString1:
/* Store Values to specific structure */
break;
case VAR_KeyString1:
/* Store Values to specific structure */
break;
default:
break;
}
fclose(fp);
return(0);
}
}
The above code is only able to store the first values which is under [KeyString1] and then it will exit the loop. 上面的代码只能存储在[KeyString1]下的第一个值,然后它将退出循环。 Looking for any suggestion for my problem. 寻找关于我的问题的任何建议。
if (temp == " ") {
continue;
}
is a newline check so it should be 是换行检查,所以应该
if (temp[0] == '\n') {
continue;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.