从fscanf接收奇怪的数字 - int转换?

时间:2014-02-14 02:29:17

标签: c scanf

我是C的新手并且在这里遇到问题。我试图通过以下脚本传递文件numbers.in。 numbers.in包含以下两行:

12,34,56789
123456,789,0123

我试图识别逗号划分。

#include <stdio.h>
void main(int argc, char *argv[])
{      
  int   p ,n ,x ;   //Converted ints.

  while ( fscanf(stdin,"%d,%d,%d\n",&p,&n,&x) == 3 );
  {
    printf("got the sequence (%d,%d,%d)\n",x,p,n);
  }
}

我正在运行脚本,如: ./a.out< numbers.in

目前我的脚本返回完全不同的数字!我在这做错了什么?文件是否将它们作为字符发送,所以我需要以某种方式转换为int? (我尝试保存为字符然后将字符转换为字符并且还有奇怪的数字 - 但不同的奇怪数字!)

已解决,分号使用不良&gt; _&lt;

1 个答案:

答案 0 :(得分:3)

  while ( fscanf(stdin,"%d,%d,%d\n",&p,&n,&x) == 3 ); <-- remove this semicolon
相关问题