读取和写入二进制文件中的缓冲区

发布于 2024-12-09 07:48:45 字数 504 浏览 0 评论 0原文

这是我现在的代码:

#include <stdio.h>
#include "readwrite.h"

int main ()
{   FILE* pFile;
    char buffer[] = {'x' ,'y','z',0};
    pFile = fopen("C:\\Test\\myfile.bin","wb");

    if (pFile ){
        fwrite(buffer,1,sizeof(buffer),pFile); 

    printf("The buffer looks like: %s",buffer);
  }
    else
    printf("Can't open file");


   fclose(pFile);
   getchar();
   return 0;
}

我正在尝试编写一些内容来验证我写入文件,然后从文件中读取并验证我从文件中读取。如何最好地做到这一点?我还需要找到一种方法将相同的内容写入两个不同的文件。这可能吗?

Here is my code as of now:

#include <stdio.h>
#include "readwrite.h"

int main ()
{   FILE* pFile;
    char buffer[] = {'x' ,'y','z',0};
    pFile = fopen("C:\\Test\\myfile.bin","wb");

    if (pFile ){
        fwrite(buffer,1,sizeof(buffer),pFile); 

    printf("The buffer looks like: %s",buffer);
  }
    else
    printf("Can't open file");


   fclose(pFile);
   getchar();
   return 0;
}

I'm trying to write something verify i wrote to the file and then read from the file and verify i read from the file. How best is there to do this? I also need to figure out a way to write the same thing to 2 different files. Is this even possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

醉态萌生 2024-12-16 07:48:45

我想你正在寻找这样的东西:

FILE* pFile;
char* yourFilePath  = "C:\\Test.bin";
char* yourBuffer    = "HelloWorld!";
int   yorBufferSize = strlen(yourBuffer) + 1;

/* Reserve memory for your readed buffer */
char* readedBuffer = malloc(yorBufferSize);

if (readedBuffer==0){
    puts("Can't reserve memory for Test!");
}

/* Write your buffer to disk. */
pFile = fopen(yourFilePath,"wb");

if (pFile){
    fwrite(yourBuffer, yorBufferSize, 1, pFile);
    puts("Wrote to file!");
}
else{
    puts("Something wrong writing to File.");
}

fclose(pFile);

/* Now, we read the file and get its information. */
pFile = fopen(yourFilePath,"rb");

if (pFile){
    fread(readedBuffer, yorBufferSize, 1, pFile);
    puts("Readed from file!");
}
else{
    puts("Something wrong reading from File.\n");
}

/* Compare buffers. */
if (!memcmp(readedBuffer, yourBuffer, yorBufferSize)){
    puts("readedBuffer = yourBuffer");
}
else{
    puts("Buffers are different!");
}

/* Free the reserved memory. */
free(readedBuffer);

fclose(pFile);
return 0;

问候

I think you are looking for something like this:

FILE* pFile;
char* yourFilePath  = "C:\\Test.bin";
char* yourBuffer    = "HelloWorld!";
int   yorBufferSize = strlen(yourBuffer) + 1;

/* Reserve memory for your readed buffer */
char* readedBuffer = malloc(yorBufferSize);

if (readedBuffer==0){
    puts("Can't reserve memory for Test!");
}

/* Write your buffer to disk. */
pFile = fopen(yourFilePath,"wb");

if (pFile){
    fwrite(yourBuffer, yorBufferSize, 1, pFile);
    puts("Wrote to file!");
}
else{
    puts("Something wrong writing to File.");
}

fclose(pFile);

/* Now, we read the file and get its information. */
pFile = fopen(yourFilePath,"rb");

if (pFile){
    fread(readedBuffer, yorBufferSize, 1, pFile);
    puts("Readed from file!");
}
else{
    puts("Something wrong reading from File.\n");
}

/* Compare buffers. */
if (!memcmp(readedBuffer, yourBuffer, yorBufferSize)){
    puts("readedBuffer = yourBuffer");
}
else{
    puts("Buffers are different!");
}

/* Free the reserved memory. */
free(readedBuffer);

fclose(pFile);
return 0;

Regards

莫多说 2024-12-16 07:48:45

读取缓冲区的程序几乎相同,除了“rb”用于读取二进制文件和 fread() 而不是 fwrite()

请记住,您必须知道缓冲区有多大您将要读取 is 并准备好一些大小合适的内存来接收它

The program to read a buffer is almost the same except "rb" for read binary and fread() instead of fwrite()

Remember you will have to know how big the buffer you are going to read is and have some memory of the right size ready to receive it

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