DJGPP 的读取错误
在 DOS 上使用 DJGPP 读取二进制文件时,此代码挂起。 当进行 fread 调用时会发生这种情况。 如果调用被删除,则程序成功运行。 相同的代码在 Visual C++ 2008 中运行良好。 有人在使用 djgpp 时遇到过类似的问题吗? 我错过了一些非常简单的事情吗?
char x;
string Filename = "my.bin" ;
fp = fopen(Filename.c_str(),"rb");
if (fp == NULL)
{
cout << "File not found" << endl ;
}
if (fseek (fp, 0, SEEK_END) != 0)
{
cout <<"End of File can't be seeked";
return -1;
}
if ( (fileLength = ftell(fp)) == -1)
{
cout <<"Can't read current position of file";
return -1;
}
if (fseek (fp, 0, SEEK_SET) != 0)
{
cout <<"Beginning of File can't be seeked";
return -1;
}
if (fread(&x,sizeof(x),1,fp) != sizeof(x))
{
cout <<"file not read correctly";
return -1;
}
While reading a binary file using DJGPP on DOS this code hangs.
This happens when the fread call is made. If the call is removed then the program runs successfully.
The same code runs fine through Visual C++ 2008.
Has anyone experienced similar issues with djgpp ?
Am I missing out on something really simple ?
char x;
string Filename = "my.bin" ;
fp = fopen(Filename.c_str(),"rb");
if (fp == NULL)
{
cout << "File not found" << endl ;
}
if (fseek (fp, 0, SEEK_END) != 0)
{
cout <<"End of File can't be seeked";
return -1;
}
if ( (fileLength = ftell(fp)) == -1)
{
cout <<"Can't read current position of file";
return -1;
}
if (fseek (fp, 0, SEEK_SET) != 0)
{
cout <<"Beginning of File can't be seeked";
return -1;
}
if (fread(&x,sizeof(x),1,fp) != sizeof(x))
{
cout <<"file not read correctly";
return -1;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不明白“fp”是什么。 我只需假设它是“FILE * fp;”。
我没有看到你实际上包括了
stdio.h >,并且必须假设您这样做。
我没有看到你实际上包括了
iostream> 并声明“using namespace std;”,并且必须假设您这样做。
我没有看到 fread() 调用之后会发生什么来告诉您调用是否成功。
当一段代码让您目瞪口呆时,您应该做的第一件事是,将错误代码实际减少到绝对但完整的最小值以重现错误。
事实可能(而且通常确实如此)问题甚至并不在您想象的地方。
话虽这么说,我会尝试替换
为
Using 'perror()' 而不是自制的cout 消息,为您提供有关任何错误原因的附加信息。 使用“fgetc()”将向您显示该文件实际上确实包含您认为它包含的内容,并且您的问题并不是由于对单个字节使用 fread() 的情况有些不常见。
然后回来报告。
I don't see what 'fp' is. I just have to assume it's 'FILE * fp;'.
I don't see that you actually include < stdio.h >, and have to assume you do.
I don't see that you actually include < iostream > and declare 'using namespace std;', and have to assume you do.
I don't see what comes after the fread() call that could tell you if call succeeded.
Going through the process of actually reducing your faulty code to the absolute but complete minimum to reproduce the error is the first thing you should do when a piece of code has you dumbfounded.
It might (and usually does) turn out that the problem isn't even where you thought it is.
That being said, I'd try replacing
with
Using 'perror()' instead of homebrewn cout messages gives you additional information on the cause of any error. Using 'fgetc()' will show you that the file actually does contain what you think it does, and that your problems are not due to the somewhat uncommon use of fread() for a single byte.
Then report back.
fread 将指针作为第一个参数。 如果您只需读取一个字符,
char x;
就可以了,但要传递 x 的地址。由于 sizeof char 始终为 1 (根据定义),您可以很好地编写:
fread takes a pointer as the first parameter. If you only have to read in one character,
char x;
is fine, but pass the address of x instead.and since sizeof char is always 1 (by definition) you can very well write: