读取和写入缓冲区问题

发布于 2024-12-10 09:29:50 字数 2770 浏览 0 评论 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;
    }   
}

ma​​in.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

我的问题是:为什么当我使用调试模式单步执行时没有得到结果,以及为什么我无法看到缓冲区被正确填充。这“几乎”就像我的 writes/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, fseeks to my desired file size -1, then writes a byte. It then fseeks 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 writes/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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

傲世九天 2024-12-17 09:29:50

以下是我看到的问题:

  1. f用“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 种不同方法来电。您可以选择任何一种方式(最好是第三种)并在任何地方使用。

Here are the problems I see:

  1. You fopen the file with "wb", which would truncate the file if existing or create a new one if not existing. So how do you expect to seek to 20th byte?
  2. Where is SIZE_OF_FILE set or more importantly is it set correctly?

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.

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;

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文