问题描述
我在Windows XP 5.1(SP2)上使用MS Visual C ++ 6.0。
我不确定这是C,C ++还是MS问题,但fscanf已经
随机挂在我身上。我打电话数百,如果不是数千,
次,但它挂在不同的地方,使用相同的数据。
违规代码如下。
ReadFile(char * csFileName)
{
float fFloat1 ,fFloat2;
long lLong1,lLong2,lNum,lLastX = iColumns-1,lLastY = iRows-1;
int iRead;
FILE * fpInFile;
if((fpInFile = fopen(csFileName," r"))== NULL)return
ErrorOpeningFile(csFileName);
do
{
//随机挂在以下
行
iRead = fscanf(fpInFile,"%d%d%f%f%d"& lLong1,& lLong2,& fFloat1,
& fFloat2,& lNum );
if(iRead == 0 || iRead == EOF)break;
} while(lX< lLastX || lY< lLastY);
}
我想知道我是否应该只进行二进制读取,以便对发生的事情有更多的控制权。 br />
非常感谢任何协助,
彼得。
6月18日上午10点19分,PeterOut< MajorSetb ... @ excite.comwrote:
亲爱的Peter,
您没有关闭文件。
使用fclose关闭文件。
谢谢和问候,
Amal P.
亲爱的彼得,
您没有关闭文件。
使用fclose关闭文件。
谢谢和问候,
Amal P.-隐藏引用文字 -
- 显示引用text -
对不起。我正在关闭文件
fclose(fpInFile);
但忘记在我发布的示例代码中包含它。
问题,不合时宜地在其他地方。
谢谢,
彼得。
%d用于类型''int''。对于''long''类型,使用%ld。
还要确保你已经#include< stdio.h为
声明' 'fscanf()''。
-Mike
I am using MS Visual C++ 6.0 on Windows XP 5.1 (SP2).
I am not sure if this is a C, C++ or MS issue but fscanf has been
randomly hanging on me. I make the call hundreds, if not thousands,
of times but it hangs in different places with the same data. The
offending code follows.
ReadFile(char *csFileName)
{
floatfFloat1, fFloat2;
longlLong1, lLong2, lNum, lLastX = iColumns-1, lLastY =iRows-1;
intiRead;
FILE *fpInFile;
if ((fpInFile= fopen(csFileName, "r")) == NULL) return
ErrorOpeningFile(csFileName);
do
{
// It randomly hangs on the followinf
line
iRead=fscanf(fpInFile, "%d%d%f%f%d", &lLong1, &lLong2, &fFloat1,
&fFloat2, &lNum);
if (iRead==0 || iRead==EOF) break;
} while (lX < lLastX || lY < lLastY);
}
I am wondering if I should just do binary reading so as to have more
control over what is going on.
Many thanks in advance for any assistance,
Peter.
Dear Peter,
Your are not closing the file.
Use fclose for closing the file.
Thanks and regards,
Amal P.
Dear Peter,
Your are not closing the file.
Use fclose for closing the file.
Thanks and regards,
Amal P.- Hide quoted text -
- Show quoted text -
Sorry. I am closing the file with
fclose(fpInFile);
but forgot to include that in the sample code that I posted. The
problem, unfotruntately, lies elsewhere.
Thanks,
Peter.
%d is for type ''int''. For type ''long'', use %ld.
Also make sure you''ve #included <stdio.hfor the
declaration of ''fscanf()''.
-Mike
这篇关于fscanf挂起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!