fscanf 读取零字节,而 perror( " " ) 返回"success"



我正在Ubuntu上使用NetBeans 8进行开发。

以下代码运行,但在函数末尾,scanResult=0,lNumGenes=0和perror("(;回报成功。

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int examineCompactFile(char * csCompactFileName, BOOL *isValid)
{
int     i, scanResult;
long    lNumGenes;
FILE * fpCompactFile;
if ((fpCompactFile=fopen(csCompactFileName , "r")) == NULL) return -1;
if ((scanResult=fscanf(fpCompactFile, "%ld", &(lNumGenes))) == EOF)
return -1;
perror("");
fprintf(stderr, "scanResult=%dn", scanResult);
fprintf(stderr, "lNumGenes=%dn", lNumGenes);
return 0;
}

当我做时

od /var/www/Libraries/input.cdf | more

在inut文件上,我得到了

0000000 000103 000000 000001 000000 005170 005014 051105 000034
0000020 000004 000000 000000 000000 043101 054106 032455 026521
0000040 031061 000063 000000 000000 000000 000000 000000 000000
0000060 000000 000000 000000 000000 000000 000000 000000 000000
*
0000120 000000 000000 000000 000000 043101 054106 032455 026521
0000140 032464 000066 000000 000000 000000 000000 000000 000000
0000160 000000 000000 000000 000000 000000 000000 000000 000000
*

我预计scanResult将是一个长整数的大小,lNumGenes将为非零。

  1. fscanf返回成功转换的次数,而不是读取的字节数。

  2. fscanf仅在存在i/o错误时设置errno。转换失败并不是那种错误。除非fscanf返回EOF(表示文件结束或读取错误(,否则不应尝试调用perror您已验证这是ferr的读取错误。

  3. 您的输入文件似乎是一个二进制文件。fscanf用于提取格式的数据(即文本(。

最新更新