我自己写的代码如下,运行的时候总是报错,fgets error,我真看不出来到底哪个地方除了错,求大神指教!
/* ------------------------------------------------------------------- *
* ---------------------------- includes ----------------------------- *
* ------------------------------------------------------------------- */
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <time.h>
#include <malloc.h>
#include <assert.h>
/* ------------------------------------------------------------------- *
* ----------------------------- structs ----------------------------- *
* ------------------------------------------------------------------- */
typedef struct vector
{
double v[4];
}VECTOR;
typedef struct tle_ascii
{
char l[3][71];
}TLE_ASCII;
/*------------------------------------------------------------------- *
* ------------------------------ num_of_lines---------------------------- *
* ------------------------------------------------------------------- */
long num_of_lines(FILE *testdatafile)
{
long i = 0;
char inputchar[1024];
while(1)
{
char *ret = fgets(inputchar, 1024, testdatafile);
i = i + 1;
if(NULL == ret)
{
printf("i = %ld\n", (i - 1) / 3);
fclose(testdatafile);
break;
}
}
return (i - 1) / 3;
}
/* ------------------------------------------------------------------- *
* ------------------------------ getdata---------------------------- *
* ------------------------------------------------------------------- */
void getdata(FILE *testdatafile, struct tle_ascii *sat_data)
{
char *ret;
char inputchar[72];
long j = 0;
printf("in getdata function, sat_data is %x\n", sat_data);
while(!(feof(testdatafile)))
{
if(NULL == fgets(inputchar, 72, testdatafile))
{
printf("fgets ERROR!!!\n");
exit(-1);
}
printf("ret = %x\n", ret);
printf("inputchar = %s\n", inputchar);
if(NULL == testdatafile)
{
printf("fopen ERROR!!!\n");
exit(-1);
}
if(0 == strncmp("IRIDIUM", inputchar, 7))
{
printf("This is head data of each group: %s\n", inputchar);
}
else if(0 == strncmp("1", inputchar, 1))
{
strncpy(sat_data[j].l[1], inputchar, 70);
printf("sat_data[%d].l[1] = %s\n", sat_data[j].l[1]);
}
else if(0 == strncmp("2", inputchar, 1))
{
strncpy(sat_data[j].l[2], inputchar, 70);
printf("sat_data[%d].l[2] = %s\n", sat_data[j].l[2]);
j++;
}
}
fclose(testdatafile);
}
/* ------------------------------------------------------------------- *
* ----------------------------- main function ----------------------------- *
* ------------------------------------------------------------------- */
int main()
{
char *indata = "iridium-33-debris.txt";
FILE *testdatafile;
testdatafile = fopen(indata, "rb");
if(testdatafile == NULL)
{
printf("testdatafile fopen ERROR!!!\n");
}
struct tle_ascii *sat_data = NULL;
struct vector *pos = NULL;
struct vector *vel = NULL;
long num = num_of_lines(testdatafile);
printf("number of lines in the datafile is = %ld\n", num);
printf("Please input the number above:\n");
scanf("%ld", &num);
sat_data = (struct tle_ascii *)malloc(sizeof(struct tle_ascii) * num);
if(NULL == sat_data)
{
printf("struct tle_ascii malloc ERROR!\n");
exit(-1);
}
pos = (struct vector *)malloc(sizeof(struct vector) * num);
if(NULL == pos)
{
printf("struct vector malloc ERROR!\n");
exit(-1);
}
vel = (struct vector *)malloc(sizeof(struct vector) * num);
if(NULL == vel)
{
printf("struct vector malloc ERROR!\n");
exit(-1);
}
getdata(testdatafile, sat_data);
return 0;
}
voidgetdata(FILE*testdatafile,structtle_ascii*sat_data)
fgets(inputchar,1024,testdatafile);
函数接口,和调用方法,你觉得对吗?
有没有看warning???肯定有。无非你认为那玩意不阻挡你编译完成不介意而已。
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。