开发者社区> 问答> 正文

动态获取数据文件行数,以此数值分配结构体数组,但是读取数据文件时发生错误:fge?报错

我自己写的代码如下,运行的时候总是报错,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);


 /* 
  this part is about some calculating, so I don't put code here.....
        */

 return 0;
}

 

 

展开
收起
爱吃鱼的程序员 2020-06-22 19:51:19 604 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    voidgetdata(FILE*testdatafile,structtle_ascii*sat_data)

    fgets(inputchar,1024,testdatafile);

    函数接口,和调用方法,你觉得对吗?

    有没有看warning???肯定有。无非你认为那玩意不阻挡你编译完成不介意而已。


    我倒。。。我引用的代码行错了。。。。。哈哈。是听了你的话以后把一些指针打印的东西去掉了,也添加了头文件stdlib.h以后,才不报任何warning的,但是那几个函数没有修改。野鬼叔叔,编译这个程序的时候没有任何warning,但是执行的时候就会报错fetserror……,我真不知道错在哪里,因为我用同样的代码,把获取数据行数那个行数返回值直接替换为一个具体的数字,比如说450,就没有错误,我实在没办法啦,野鬼叔叔~~~~到底是怎么回事?这个问题困扰了我两天了……有木有人呢?不排版,无高亮,木人看这个是Linux下的代码,拷出来就已经没有高亮了,排版贴上也乱了……问题已经解决了,是我粗心造成的,在获取行数的汉书里已经把数据文件fclose了,下面再继续调用getdata函数时没有再次打开。
    2020-06-22 19:51:35
    赞同 展开评论 打赏
问答地址:
问答排行榜
最热
最新

相关电子书

更多
用计算和数据去改变整个世界 立即下载
HBase2.0重新定义小对象实时存取 立即下载
低代码开发师(初级)实战教程 立即下载