尝试分块读取文件但无法按线性顺序读取?
因此,我尝试以 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看来您希望
将读取的数据复制到 myBufferPointer。它不会
因为你没有显示 myBufferPointer 是什么,我会猜测它是这样的
你应该做的是这个
注意,如果你只想下一个块,你不需要寻找记住
,如果你想像字符串一样打印它你需要将其清零终止,read 不会为你做到这一点。
It seems that you are expecting this
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
What you should do is this
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.
查看这个尝试执行您所描述的操作的示例。它可能会帮助您更好地了解运算符和一些标准调用在 C 中的工作原理。
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.