I’ve a bad problem. I’m trying to write to a file via filedescriptor and memalign. I can write to it but only something like an wrong encoded char is written to a file.
Here’s my code:
fdOutputFile = open(outputFile, O_CREAT | O_WRONLY | O_APPEND | O_DIRECT, 0644)
void writeThis(char* text) {
while (*text != '\0') {
// if my internal buffer is full -> write to disk
if (buffPositionOutput == outputbuf.st_blksize) {
posix_memalign((void **)&bufferO, outputbuf.st_blksize, outputbuf.st_blksize);
cout << "wrote " << pwrite(fdOutputFile, bufferO, outputbuf.st_blksize, outputOffset*outputbuf.st_blksize) << " Bytes to disk." << endl;
buffPositionOutput = 0;
++outputOffset;
}
// buffer the incoming text...
bufferO[buffPositionOutput] = *text;
++text;
++buffPositionOutput;
}
}
I think it’s the alignment – can someone help me?
It writes to the file but not the correct text, just a bunch of ‘[]’-chars.
Thanks in advance for your help!
Looking at your program, here is what happens:
buffer0+buffPositionOutput(Which is where, precisely? I don’t know based on the code you give.) up tobuffer0+outputbuf.st_blksizewith data.buffer0pointer toposix_memalign, which ignores its current value and overwrites it with a pointer tooutputbuf.st_blksizebytes of newly-allocated memory.This won’t work, obviously. You probably want to initialize your buffer via
posix_memalignat the top of your function, and then just overwrite the block’s worth of data in it as you use your aligned buffer to repeatedly write data into the file. (Resetbuffpositionoutputto zero after each time you write data, but don’t re-allocate.) Make sure youfreeyour buffer when you are done.Also, why are you using
pwriteinstead ofwrite?Here’s how I would implement
writeThis(keeping your variable names so you can match it up with your version):(For speed, you might consider using
memcpycalls instead of a loop. You would need to know the length of the data to write ahead of time though. Worry about that after you have a working solution that does not leak memory.)