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);
CodePudding user response:
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.
CodePudding user response:
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);
}
}