尝试分块读取文件但无法按线性顺序读取?

发布于 2025-01-15 05:48:24 字数 1233 浏览 4 评论 0原文

因此,我尝试以 128 字节为单位读取名为 f1 的输入文件。读取前 128 个字符后,我使用 smooth() 将指针移动到第 129 个字符,但它仍然只读取前 128 个字符。你能告诉我我做错了什么吗?读取字符后,我想将它们存储在 myBufferPointer 中。

这就是我在共享内存中创建缓冲区指针的方法。

 /* name of the shared memory buffer */
char* myBuffer = "MyBuff"; 
/* shared memory file descriptor  */
int myBufferFileDesc;
/* pointer to shared memory obect */
char *myBufferPointer;
/* create the shared memory object */

myBufferFileDesc = shm_open(myBuffer,O_CREAT | O_RDWR,0666);
/* configure the size of the shared memory object */
// the size here will be buffer size * chunk size 
ftruncate(myBufferFileDesc, 3072);
/* memory map the shared memory object */
myBufferPointer = (char *)
mmap(0, SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, myBufferFileDesc, 0);

目标是使用 read() 将输入文件写入共享内存。

   int next_produced;
   char tempBuffer[128];
   string str;

   next_produced = read(f1, tempBuffer, 128);

   myBufferPointer += tempBuffer;

       

   printf("The conents of buffer the first time are %s\n", myBufferPointer);

 
   lseek(f1, 129, SEEK_CUR);
   read(f1, tempBuffer, 128);

   myBufferPointer += tempBuffer;



   printf("The conents of second time are %s\n", myBufferPointer);

So I'm trying to read an input file called f1 in chunks of 128 bytes. After reading the first 128 chars I move the pointer using sleek() to 129th char, but it still only reads the first 128 chars. Could you tell me what I'm doing wrong? After reading the chars, I want to store them in myBufferPointer.

This is how I create the bufferpointer in the shared memory.

 /* name of the shared memory buffer */
char* myBuffer = "MyBuff"; 
/* shared memory file descriptor  */
int myBufferFileDesc;
/* pointer to shared memory obect */
char *myBufferPointer;
/* create the shared memory object */

myBufferFileDesc = shm_open(myBuffer,O_CREAT | O_RDWR,0666);
/* configure the size of the shared memory object */
// the size here will be buffer size * chunk size 
ftruncate(myBufferFileDesc, 3072);
/* memory map the shared memory object */
myBufferPointer = (char *)
mmap(0, SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, myBufferFileDesc, 0);

The goal is to write a input file into the shared memory using read().

   int next_produced;
   char tempBuffer[128];
   string str;

   next_produced = read(f1, tempBuffer, 128);

   myBufferPointer += tempBuffer;

       

   printf("The conents of buffer the first time are %s\n", myBufferPointer);

 
   lseek(f1, 129, SEEK_CUR);
   read(f1, tempBuffer, 128);

   myBufferPointer += tempBuffer;



   printf("The conents of second time are %s\n", myBufferPointer);

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

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

发布评论

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

评论(2

千纸鹤 2025-01-22 05:48:24

看来您希望

myBufferPointer += tempBuffer;

将读取的数据复制到 myBufferPointer。它不会

因为你没有显示 myBufferPointer 是什么,我会猜测它是这样的

char buffer[1000];
char * myBufferPointer = buffer;

你应该做的是这个

next_produced = read(f1, myBufferPointer, 128); // read into buffer
myBufferPointer +=  next_produced;              // move pointer up by amount read

next_produced = read(f1, myBufferPointer, 128); // read into buffer
myBufferPointer +=  next_produced;              // move pointer up by amount read

注意,如果你只想下一个块,你不需要寻找记住

,如果你想像字符串一样打印它你需要将其清零终止,read 不会为你做到这一点。

It seems that you are expecting this

myBufferPointer += tempBuffer;

to copy the read data to myBufferPointer. It will not

Since you dont show what myBufferPointer is I am going to gues that its like this

char buffer[1000];
char * myBufferPointer = buffer;

What you should do is this

next_produced = read(f1, myBufferPointer, 128); // read into buffer
myBufferPointer +=  next_produced;              // move pointer up by amount read

next_produced = read(f1, myBufferPointer, 128); // read into buffer
myBufferPointer +=  next_produced;              // move pointer up by amount read

Note that you dont need to seek if you just wan tth enext chunk

Remeber that if you want to printf this like a string you need to zero terminate it, read will not do that for you.

梦里的微风 2025-01-22 05:48:24

查看这个尝试执行您所描述的操作的示例。它可能会帮助您更好地了解运算符和一些标准调用在 C 中的工作原理。

#include <stdio.h>
#include <stdlib.h>
#include <memory.h>

int main() {
   const size_t bufferSize = 128;
   const int chunkCount = 7;

   char fileName[] = "tpm.txt";
   char tempBuffer[bufferSize + 1] = {0,};

   /* Create sample input file */
   FILE* f1 = fopen(fileName,"wb");
   if (!f1) {
      printf("Cannot open file \"%s\" for writing.\n", fileName);
      exit(1);
   }

   printf("* Writing %d chunks of %lld bytes to \"%s\".\n\n",chunkCount,bufferSize,fileName);

   for (int i=1;i<= chunkCount;i++) {
      memset(tempBuffer, '0'+i, bufferSize );
      fwrite(tempBuffer, sizeof(char), bufferSize, f1);
   }
   fclose(f1);

   /* Read back input file */
   f1 = fopen(fileName, "rb");
   if (!f1) {
      printf("Cannot open file \"%s\" for reading.\n", fileName);
      exit(1);
   }

   printf("* Reading %lld byte chunks from \"%s\".\n\n", bufferSize, fileName);

   size_t next_produced=1;
   memset(tempBuffer, '0', bufferSize);
   tempBuffer[bufferSize] = 0;

   for (int i = 1; ; i++) {
      next_produced = fread(tempBuffer, sizeof(char), bufferSize, f1);
      
      if ( 0 >= next_produced )
         break;

      printf("The contents of buffer are:\n%s\n", tempBuffer);
   }
}

Look at this sample that tries to do what you describe. It might help you see better, how operators and some standard calls work in C.

#include <stdio.h>
#include <stdlib.h>
#include <memory.h>

int main() {
   const size_t bufferSize = 128;
   const int chunkCount = 7;

   char fileName[] = "tpm.txt";
   char tempBuffer[bufferSize + 1] = {0,};

   /* Create sample input file */
   FILE* f1 = fopen(fileName,"wb");
   if (!f1) {
      printf("Cannot open file \"%s\" for writing.\n", fileName);
      exit(1);
   }

   printf("* Writing %d chunks of %lld bytes to \"%s\".\n\n",chunkCount,bufferSize,fileName);

   for (int i=1;i<= chunkCount;i++) {
      memset(tempBuffer, '0'+i, bufferSize );
      fwrite(tempBuffer, sizeof(char), bufferSize, f1);
   }
   fclose(f1);

   /* Read back input file */
   f1 = fopen(fileName, "rb");
   if (!f1) {
      printf("Cannot open file \"%s\" for reading.\n", fileName);
      exit(1);
   }

   printf("* Reading %lld byte chunks from \"%s\".\n\n", bufferSize, fileName);

   size_t next_produced=1;
   memset(tempBuffer, '0', bufferSize);
   tempBuffer[bufferSize] = 0;

   for (int i = 1; ; i++) {
      next_produced = fread(tempBuffer, sizeof(char), bufferSize, f1);
      
      if ( 0 >= next_produced )
         break;

      printf("The contents of buffer are:\n%s\n", tempBuffer);
   }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文