读写缓冲区问题
Reading and writing a buffer issues
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 : %dn",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 : %dnn",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
s到我想要的文件大小-1,然后写入一个字节。然后将文件中的fseek
s移到给定的偏移量。如果它成功地写入了nbytes
,它打印出一些东西,然后关闭文件。否则输出无法写入并关闭文件。
在我的main中,我只是测试我的代码是否写了我想要的东西,我有两个for循环,后面有两次写操作。在此之后,我也在做test.read
,但我不确定为什么我不能看到正确的结果,我认为我应该在编译时进入调试模式。
仅供参考,read
类函数几乎与write
相同,但当然fread
。
我的问题是:为什么我没有得到的结果,当我通过使用调试模式,为什么我不能看到缓冲区被正确填充。这"几乎"就像我的write
s/read
没有真正发生。
EDIT:我"试图做的"是用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("写入文件(pFile pvsrc 1 1);返回——%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
我看到的问题如下:
- 你用"wb"打开文件,如果文件存在,它将截断文件,如果不存在,它将创建一个新的文件。那么,如何查找第20个字节呢?
- 哪里是SIZE_OF_FILE设置或更重要的是它设置正确?
建议:永远不要忘记检查你调用的所有函数的返回码。把它们打印出来。就像你在这里做的:if (!pFile){ /*...*/}
。
fseek(pFile,SIZE_OF_FILE-1,SEEK_SET);
fwrite(pvsrc,1,1,pFile);
fseek(pFile,offset,SEEK_SET);
// should be
std::cout << "fseek(pFile,SIZE_OF_FILE-1,SEEK_SET); returned -- " << fseek(pFile,SIZE_OF_FILE-1,SEEK_SET) << std::endl;
printf( "fwrite(pvsrc,1,1,pFile); returned -- %d", fwrite(pvsrc,1,1,pFile));
int retCode;
if((retCode = fseek(pFile,offset,SEEK_SET)) != 0)
std::cout << "fseek(pFile,offset,SEEK_SET) returned non-zero code -- " << retCode << std::endl;
不要感到困惑,我刚刚展示了3种不同的方法为3个不同的调用打印调试信息。你可以选择任何一种方法(最好是第三种),并在任何地方使用。
- 我正在尝试使用回溯来解决 N queen 问题,但在编译时它会给出运行时错误(动态堆栈缓冲区溢出)
- 绘制的 OpenGL 点消失,绘制调用和交换缓冲区问题
- 将 char* 推送到矢量时出现问题,但在每次迭代后,它会将指向相同值缓冲区的指针添加到矢量中
- 缓冲区溢出 - 似乎无法找到问题
- 如何解决此缓冲区溢出问题?
- 设备驱动程序内存缓冲区处理器缓存问题
- GetProcessImageFileNameA 出现问题,缓冲区大小不足
- C++:循环缓冲区的问题
- Sprintf 缓冲区问题,错误分配给字符数组
- 这个流缓冲区有什么问题?
- 在 Python 中使用 OpenCV 将打包的 BGRA 图像缓冲区转换为 RGB 时遇到问题
- 协议缓冲区;将数据保存到磁盘并重新加载问题
- 字符串副本表示缓冲区太小,strcpy_s源问题
- 调整窗口大小后交换渲染缓冲区会导致问题
- 了解 DXGI DirectX 11 桌面复制以获取缓冲区或阵列的问题
- 尝试在 OSX 中创建多个帧缓冲区对象而不是 Linux 时出现问题
- 顶点缓冲区问题
- C++丢弃输入缓冲区问题中剩下的换行符
- mmap共享缓冲区读取问题
- 协议缓冲区,让C#与C++对话:类型问题和模式问题