的test1.c:打开文件会导致不同的结果
int main(int argc, char *argv[])
{
FILE* fp = fopen("test.txt","rw");
int n,m,q;
scanf(" %d%d%d",&n,&m,&q);
struct test_t test;
while (fscanf(fp,"%d%s%d%d%d%d%d%d%d%*d",&test.line,test.data,&test.number[0],&test.number[1],&test.number[2],&test.number[3],&test.number[4],&test.number[5],&test.number[6])!=EOF) {
//do something.....
}
return 0;
}
的test2.c中:
int main(int argc, char *argv[])
{
int n,m,q;
scanf(" %d%d%d",&n,&m,&q);
struct test_t test;
FILE* fp = fopen("test.txt","rw");
while (fscanf(fp,"%d%s%d%d%d%d%d%d%d%*d",&test.line,test.data,&test.number[0],&test.number[1],&test.number[2],&test.number[3],&test.number[4],&test.number[5],&test.number[6])!=EOF) {
//do something....
}
return 0;
}
test_t的定义:
struct test_t {
int line;
char* data;
int number[7];
};
我的测试。 txt:
141 2015-12-05 19 16 35 06 34 46 09 00
124 2015-12-08 49 25 10 09 40 48 32 00
143 2015-12-10 09 29 24 47 32 34 42 00
当我使用test1.c时,我得到了分段错误。但是当我使用test2.c时,它运行良好。只改变了FILE* fp = fopen("test.txt","rw");
这一行。引起这种差别的原因是什么。
您的代码不会编译,因为编译器不知道'size_t'的大小。 –
@AshishAhuja我没有发布'test_t'的定义,我现在会发布它 – icecity96
你为什么不发布它?总是发布编译的代码! –