【问题标题】:Error when reading file using fscanf使用 fscanf 读取文件时出错
【发布时间】:2014-10-16 12:46:15
【问题描述】:

我还是不明白为什么这个 printf 打印了很多 0 而 i++ 没有效果

    int i=0;
char datos[5]="";

while((fscanf(entry_file,"%5s",datos))==1)
{
    //Buffer para @
    char x;

    if((strcmp(datos,"DATOS"))==0)
    {
        fscanf(entry_file,"%lf%c%lf%c%lf%c%lf%c%lf%c%lf%c%lf",&cohetes[numCohetes].vuelos[0].aceleracionX[i],&x,&cohetes[numCohetes].vuelos[0].aceleracionY[i],&x,&cohetes[numCohetes].vuelos[0].aceleracionZ[i],
                &x,&cohetes[numCohetes].vuelos[0].altura[i],&x,&cohetes[numCohetes].vuelos[0].potencia[i],&x,&cohetes[numCohetes].vuelos[0].temperatura[i],&x,&cohetes[numCohetes].vuelos[0].tiempo[i]);
        printf("%d\n",i);
        i++;
        datos[0]='\0';
    }

}

【问题讨论】:

  • char datos[5] --> char datos[6]
  • 如果要丢弃一个字符,请使用%*c,而不是将其存储在变量中。您的问题是您忘记了字符串以 \0 结尾,并且您必须在数组中为它留出空间,正如 BLUEPIXY 评论的那样

标签: c while-loop eof scanf


【解决方案1】:

您的变量datos 太小,所以结尾的空字符会覆盖i

将声明更改为:

char datos[6]="";

【讨论】:

  • @JuanDiego ,“谢谢!” cmets 不赞成。相反,接受/赞成解决问题的答案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-07-10
  • 1970-01-01
  • 1970-01-01
  • 2020-03-20
  • 1970-01-01
  • 1970-01-01
  • 2011-03-22
相关资源
最近更新 更多