读取和写入缓冲区问题
bool CReadWrite::write(unsigned long long offset, void* pvsrc, unsigned long long nbytes)
{ int m_WriteResult;
pFile = fopen("E:\\myfile.bin","wb");
if (!pFile){
puts("Can't open file");
return false;
}
fseek(pFile,SIZE_OF_FILE-1,SEEK_SET);
fwrite(pvsrc,1,1,pFile);
fseek(pFile,offset,SEEK_SET);
printf("fseek(pFile,SIZE_OF_FILE-1,SEEK_SET); returned -- ", fseek(pFile,SIZE_OF_FILE-1,SEEK_SET));
printf( "fwrite(pvsrc,1,1,pFile); returned -- %d", fwrite(pvsrc,1,1,pFile));
printf("Current file pointer position is : %d\n",ftell(pFile));
m_WriteResult = fwrite (pvsrc, 1, nbytes, pFile);
if (m_WriteResult == nbytes){
puts("Wrote to file");
printf("The number of bytes written to the file is : %d\n\n",m_WriteResult);
fclose(pFile);
return true;
}
else{
puts("Unable to write to File.");
fclose(pFile);
return false;
}
}
main.cpp:
char* pWriteBuffer;
char* pReadBuffer;
int nbytes = 85;
int nbytes2= 10;
pWriteBuffer = new char [nbytes];
pReadBuffer = new char [nbytes];
CReadWrite test;
for(Bufferndx = 0; Bufferndx<nbytes; Bufferndx++){
pWriteBuffer[Bufferndx] = Bufferndx;
}
test.write(20,pWriteBuffer,50);
for(Bufferndx = 10; Bufferndx;Bufferndx--){
pWriteBuffer[Bufferndx] = Bufferndx;
}
test.write(30,pWriteBuffer,nbytes2);
test.read(5,pReadBuffer,85);
delete[] pWriteBuffer;
delete[] pReadBuffer;
这是我的程序的一部分,用于写入给定的缓冲区。写入函数被赋予偏移量、源以及要写入的字节数。
它首先检查文件是否能够打开,fseek
到我想要的文件大小-1,然后写入一个字节。然后它在文件中fseek
到给定的偏移量。如果它成功写入nbytes
,它会打印出一些内容,然后关闭该文件。否则它会打印出无法写入并关闭文件。
在我的主要内容中,我只是测试我的代码是否确实编写了我想要的内容,并且我有两个 for 循环,之后有两次写入。此后我还进行了 test.read
,但我不确定为什么我看不到正确的结果,我认为我应该在编译时进入调试模式。
仅供参考,read
类函数几乎与 write
相同,但当然还有 fread
。
我的问题是:为什么当我使用调试模式单步执行时没有得到结果,以及为什么我无法看到缓冲区被正确填充。这“几乎”就像我的 write
s/read
并没有真正发生一样。
编辑:我“试图做”的是用 85 字节(我最大的缓冲区)填充缓冲区。
- 我的
test.write(20,pWriteBuffer,50)
将从偏移量 20 开始写入 50 个字节。偏移量 20 -70。 - 我的第二次写入
test.write(30,pWriteBuffer,nbytes2)
将从偏移量 30 开始并写入 10 个字节。偏移 20-30。 - 第三,我读了整篇文章,我应该能够知道所有写入发生在哪里。
或者这就是计划,但我在调试时没有看到这一点。另外,也许有人可以对此有所了解,但我正在审查它,看起来我...
fseek(pFile,SIZE_OF_FILE-1,SEEK_SET);
fwrite(pvsrc,1,1,pFile);
每次我写的都是错误的...我不应该每次都找出并增大文件我写作的时间。呃
printf( "fwrite(pvsrc,1,1,pFile); 返回 -- %d", fwrite(pvsrc,1,1,pFile)); //打印出1
printf("fseek(pFile,SIZE_OF_FILE-1,SEEK_SET); 返回 -- ", fseek(pFile,SIZE_OF_FILE-1,SEEK_SET)); //打印出0
bool CReadWrite::write(unsigned long long offset, void* pvsrc, unsigned long long nbytes)
{ int m_WriteResult;
pFile = fopen("E:\\myfile.bin","wb");
if (!pFile){
puts("Can't open file");
return false;
}
fseek(pFile,SIZE_OF_FILE-1,SEEK_SET);
fwrite(pvsrc,1,1,pFile);
fseek(pFile,offset,SEEK_SET);
printf("fseek(pFile,SIZE_OF_FILE-1,SEEK_SET); returned -- ", fseek(pFile,SIZE_OF_FILE-1,SEEK_SET));
printf( "fwrite(pvsrc,1,1,pFile); returned -- %d", fwrite(pvsrc,1,1,pFile));
printf("Current file pointer position is : %d\n",ftell(pFile));
m_WriteResult = fwrite (pvsrc, 1, nbytes, pFile);
if (m_WriteResult == nbytes){
puts("Wrote to file");
printf("The number of bytes written to the file is : %d\n\n",m_WriteResult);
fclose(pFile);
return true;
}
else{
puts("Unable to write to File.");
fclose(pFile);
return false;
}
}
main.cpp:
char* pWriteBuffer;
char* pReadBuffer;
int nbytes = 85;
int nbytes2= 10;
pWriteBuffer = new char [nbytes];
pReadBuffer = new char [nbytes];
CReadWrite test;
for(Bufferndx = 0; Bufferndx<nbytes; Bufferndx++){
pWriteBuffer[Bufferndx] = Bufferndx;
}
test.write(20,pWriteBuffer,50);
for(Bufferndx = 10; Bufferndx;Bufferndx--){
pWriteBuffer[Bufferndx] = Bufferndx;
}
test.write(30,pWriteBuffer,nbytes2);
test.read(5,pReadBuffer,85);
delete[] pWriteBuffer;
delete[] pReadBuffer;
This is a part of my program that writes to a given buffer. The write function is given an offset, a source and how many bytes to write.
It first checks to see if the file was even able to open, fseek
s to my desired file size -1, then writes a byte. It then fseek
s in the file to the offset given. If it successfully wrote nbytes
, it prints out some stuff, then closes the file. Else it prints out that it was unable to write and closes the file.
In my main, I'm just testing that my code actually wrote what I wanted to and I've got two for loops that have two writes after. I'm also doing a test.read
after this but I am not sure why I'm not able to see the correct results I think I'm supposed to get in the debug mode when compiling.
Just FYI, The read
class function is almost identical to the write
but of course fread
.
My question is: Why am I not getting the results when I step through using the debug mode and why am I not able to see the buffer be filled correctly. It's "almost" like my write
s/read
is not really happening.
EDIT: What I'm "trying to do" is fill a buffer with 85 bytes (my biggest buffer).
- My
test.write(20,pWriteBuffer,50)
will start from offset 20 and write 50 bytes, aka. offset 20 -70. - My second write
test.write(30,pWriteBuffer,nbytes2)
will start at offset 30 and write 10 bytes, aka. offset 20-30. - And 3rd, I read the whole thing and I should be able to tell where all the writes occur.
Or that is the plan, but I do not see that when I debug. Also, Maybe someone can shed some light on this but I'm reviewing over it and it looks like I'm...
fseek(pFile,SIZE_OF_FILE-1,SEEK_SET);
fwrite(pvsrc,1,1,pFile);
each time I write which is wrong... I shouldn't be seeking out and making the file bigger each time I write. Ugh
printf( "fwrite(pvsrc,1,1,pFile); returned -- %d", fwrite(pvsrc,1,1,pFile)); //prints out 1
printf("fseek(pFile,SIZE_OF_FILE-1,SEEK_SET); returned -- ", fseek(pFile,SIZE_OF_FILE-1,SEEK_SET)); //prints out 0
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
以下是我看到的问题:
建议:永远不要忘记检查您正在调用的所有函数的返回码。将它们打印出来。就像您在此处所做的那样:
if (!pFile){ /*...*/}
例如,
不要混淆,我刚刚展示了打印 3 个不同的调试信息的 3 种不同方法来电。您可以选择任何一种方式(最好是第三种)并在任何地方使用。
Here are the problems I see:
Suggestions: Never forget to check the return codes of all functions you're calling. Print them out. Like you did in this:
if (!pFile){ /*...*/}
E.g.
Don't get confused, I've just shown 3 different ways of printing out debug info for 3 different calls. YOu can pick any one way (preferably 3rd one) and use everywhere.