读写缓冲区问题

Reading and writing a buffer issues

本文关键字:问题 缓冲区 读写      更新时间:2023-10-16
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

我看到的问题如下:

  1. 你用"wb"打开文件,如果文件存在,它将截断文件,如果不存在,它将创建一个新的文件。那么,如何查找第20个字节呢?
  2. 哪里是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个不同的调用打印调试信息。你可以选择任何一种方法(最好是第三种),并在任何地方使用。