[英]fscanf format specifier into structs
I have a data format like the following: 我的数据格式如下:
22/March/2014
137 8
15 16 34 8 18
17/November/2014
106 8
22 29 30 9 6
20/November/2014
169 10
50 58 38 29 1
I am trying to use fscanf to get the contents of the file and place them into the following struct: 我正在尝试使用fscanf来获取文件的内容并将其放入以下结构中:
struct date{
//This is storing the date dd/month/yyyy.
char* fullDate;
// Number of foot and wheelchair passengers.
int foot;
int wheelchair;
//number of infant, child, adult, senior and family tickets sold
int infant;
int child;
int adult;
int senior;
int family;
};
So i tried the following: 所以我尝试了以下方法:
struct date r[50];
FILE* der = fopen("C:\\Users\\Def\\Downloads\\annual_sales_data_2014.txt","r");
int i = 0;
rewind(der);
fscanf(der, "%s", r[i].fullDate);
fscanf(der, "%d %d ", &r[i].foot, &r[i].wheelchair);
fscanf(der, "%d %d %d %d",&r[i].infant, &r[i].child, &r[i].adult, &r[i].senior, &r[i].family);
printf("%s\n%d %d\n%d %d %d %d %d\n\n", r[i].fullDate, r[i].foot, r[i].wheelchair, r[i].infant, r[i].child, r[i].adult, r[i].senior, r[i].family);
fclose(der);
But i believe that fscanf is not being used right and i'm getting a bit confused by the documentation for format specifiers, I have also tried it this way. 但是我相信fscanf的使用不正确,并且格式说明符的文档让我有些困惑,我也尝试过这种方法。
fscanf(der, "%s/%s/%s\n%d %d\n%d %d %d %d %d\n", r[i].day, r[i].month, , r[i].year, &r[i].foot, &r[i].wheelchair, &r[i].infant, &r[i].child, &r[i].adult, &r[i].senior, &r[i].family);
In the above example i also catered the struct to accept 3 char*'s for the day month and year. 在上面的示例中,我还满足了该结构在月份和年份中接受3个char *的问题。 in the beginning of the format specifiers string.
在格式说明符字符串的开头。 This didn't work either so i broke it down to a smaller format to see if i could diagnose the problem.
这也不起作用,所以我将其分解为较小的格式,以查看是否可以诊断问题。
Even in the broken down example this 即使在细分的例子中
fscanf(der, "%s", r[i].fullDate);
even this breaks in the debugger and cant work out why. 即使这在调试器中中断,也无法找出原因。
Thanks. 谢谢。
fullDate
must be initialized before using. 使用前必须初始化
fullDate
。 The simplest way is: 最简单的方法是:
r[i].fullDate = malloc(whatever_size_you_need);
or you may even declare it as an array to avoid extra initialization: 或者甚至可以将其声明为数组,以避免额外的初始化:
struct date{
//This is storing the date dd/month/yyyy.
char fullDate[proper_size];
// ...
};
Of course you must ensure somehow that read string will fit into buffer. 当然,您必须以某种方式确保读取的字符串适合缓冲区。
You cannot parse the date with fscanf(der, "%s", r[i].fullDate);
您无法使用
fscanf(der, "%s", r[i].fullDate);
解析日期fscanf(der, "%s", r[i].fullDate);
because fulldate
is an uninitialized pointer. 因为
fulldate
是未初始化的指针。 You are supposed to pass the address of an array for the %s
format. 您应该以
%s
格式传递数组的地址。 Put an array in the structure instead of a pointer, such as char fullDate[30];
将数组而不是指针放在结构中,例如
char fullDate[30];
and use fscanf(der, "%29s", r[i].fullDate);
并使用
fscanf(der, "%29s", r[i].fullDate);
Note that you can parse the date components directly with: 请注意,您可以直接使用以下方法解析日期成分:
char monthName[16];
int day, year;
if (fscanf("%d/%15[^ /]/%d", &day, monthName, &year) == 3) {
/* Date parsed correctly */
} else {
/* At least one field missing, bail out */
exit(1);
}
You say you believe that fscanf
is not being used right, and that is true. 您说您相信
fscanf
未被正确使用,这是事实。 This line 这条线
fscanf(der, "%d %d %d %d", &r[i].infant, &r[i].child, &r[i].adult, &r[i].senior, &r[i].family);
is passing arguments for the 5 fields you want to fill, but it only has 4 format format specifiers. 正在传递您要填充的5个字段的参数,但它只有4个格式说明符。 Try this:
尝试这个:
fscanf(der, "%d %d %d %d %d", &r[i].infant, &r[i].child, &r[i].adult, &r[i].senior, &r[i].family);
In addition, you should always check the data fields were read properly. 另外,您应始终检查数据字段是否已正确读取。 The return value from
fscanf
would have told you that only 4 items were read. fscanf
的返回值将告诉您仅读取了4个项目。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.