铁罗(文件)== 32
有时,当我像这样打开文件时:
FILE *file = fopen(fname, "wb");
if(!file) printf("Error code: %d\n",ferror(file));
我得到的结果是 32。这是什么意思? 具体来说,对于 eMbedded Visual C++ 4.0
另外,eVC 似乎不支持 perror/errno :(
Sometimes, when I open the a file like so:
FILE *file = fopen(fname, "wb");
if(!file) printf("Error code: %d\n",ferror(file));
I get a result of 32. What does this mean? Specifically, for eMbedded Visual C++ 4.0
Also, it seems like eVC does not support perror/errno :(
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您以错误的方式使用ferror():它仅适用于有效(非空)文件句柄。 传递 NULL (在 fopen() 失败的情况下)会导致 UB,这在您的情况下通过打印随机值来体现(不捕获 NULL 指针访问表明底层平台没有内存保护)。
You are using ferror() in a wrong way: it works only on a valid (non-null) file handle. Passing it NULL (in the case fopen() fails) causes UB, which manifests in your case by printing a random value (not trapping on NULL pointer access suggests that the underlying platform does not have memory protection).
您应该尝试测试 perror 所说的错误消息是什么。 像这样使用:
它会输出这样的消息:
我想,由于当文件对象为 NULL 时您使用了ferror,因此错误 32 只是随机垃圾,正如另一篇文章提到的那样,可能缺少 NULL 指针捕获。 使用 errno/perror 获取阻止您打开文件的错误。 事实上,向ferr 传递NULL 指针是非法的。
编辑:我发现令人惊讶的是该编译器不支持 perror 和 errno 。 我建议您找到正确错误检测函数并使用它。 在这种情况下,firor 肯定不是这样的。
编辑:查看 GetLastError() 和 < a href="http://msdn.microsoft.com/en-us/library/ms679351(VS.85).aspx" rel="nofollow noreferrer">FormatMessage(),应该支持它们。 http://msdn.microsoft.com/en-us /library/ms680582(VS.85).aspx 也有一个用法示例。 尽管您可能需要替换普通 C 函数中的微软“安全字符串”函数。 (例如:StringCchPrintf -> _snprintf/sprintf)
稍微谷歌搜索表明这可能适合您。 这不是我的代码,但看起来很合理:
you should try to test what perror says the error message is. Use like this:
it will output a message like this:
I imagine, since you are using ferror when your file object is NULL, that error 32 is just random garbage as another posted mentioned, probably lack of NULL pointer trapping. use errno/perror to get the error that prevented you from opening the file. in fact, it is illegal to pass a NULL pointer to ferror.
EDIT: I find it surprising that both perror and errno aren't supported with that compiler. I recommend that you find the correct error detection functions and use that. ferror certainly isn't it in this case.
EDIT: look into GetLastError() and FormatMessage(), they should be supported. http://msdn.microsoft.com/en-us/library/ms680582(VS.85).aspx also has an example of there usage. Though you will likely need to replace the microsoft "safe string" functions which ordinary C ones. (ex: StringCchPrintf -> _snprintf/sprintf)
A little googling shows that this might work for you. It's not my code, but looks reasonable:
从此处看来,您的管道已损坏
It would appear from here that you have a broken pipe