下面是txt的一个示例。
ABS 2010-10-11 15:10:10
PER 2010-10-11 15:10:10
PER 2010-10-11 15:10:10到目前为止,我可以很容易地将数据放入结构中,而不需要像-或:这样的符号。一些C神童可以试着给我一些如何避免这个问题的提示吗?谢谢
代码如下:
#include <stdio.h>
struct Alarm
{
int year;
int month;
int day;
char Type[30];
};
void getStructInfo(struct Alarm theStruct){ //just a function to check if data is stored in the structure
printf("\n");
printf("Type: %s\n\n",theStruct.Type);
printf("Godina:%d\n",theStruct.year );
}
int main(){
struct Alarm a1,a2;
FILE *fp;
fp= fopen("podaci.txt","r");
if(fp == NULL)
{
printf("Error\n");
return 1;
}
while(1){
fscanf(fp,"%s\t%d\t%d\t%d\n",&a1.Type,&a1.year,&a1.month,&a1.day);
printf("%s\t%.4d\t%.2d\t%.2d\n",a1.Type,a1.year,a1.month,a1.day );
printf("Continue reading (y/n)\n");
char ch=getch();
if(ch == 'N' || ch== 'n')
break;
}
fclose(fp);
getStructInfo(a1);
return 0;
}输出如下: ABS 2012 -10 -11
发布于 2015-11-16 23:16:22
您应该了解fscanf是如何工作的,并使用返回值:
while( fscanf(fp, "%s%d-%u-%u%*s",&a1.Type,&a1.year,&a1.month,&a1.day)==4 )
{
...
}https://stackoverflow.com/questions/33735374
复制相似问题