Note that there are some explanatory texts on larger screens.

plurals
  1. POC - Inaccurate read() position obtained with lseek()
    primarykey
    data
    text
    <p>Creating a file, moving to location 128 and writing data but, while reading i need to read it from offset 0 rather than 128 though i am writing in 128. Can someone please point out where i am going wrong.</p> <p>After writing, printing the hex of the file. the data should be printed in dump 1 (location 128) as I am writing to that page. But it is displayed in dump 0 (location 0).</p> <p>Hexdump of the file from outside shows the data to be the location that i have written to (128).</p> <p>Is it something to do with modes or file permissions? I am working on linux os.</p> <pre><code> void readyCache() { fd = open("database.dat",O_RDWR|O_TRUNC|O_CREAT , S_IRUSR|S_IWUSR); } char* getPage(long pageNumber, int fd) { long offset; char* buffer = (char *)malloc(pageSize); offset = (pageNumber)*pageSize; lseek(fd, offset+pageSize, SEEK_SET); lseek(fd, offset, SEEK_SET); read(fd, buffer, pageSize); return buffer; } void setPage(long pageNumber,char* pageData, int fd) { long offset; offset = (pageNumber)*pageSize; lseek(fd, offset, SEEK_SET); write(fd, pageData, pageSize); } void hexdump(int fileDescriptor1, long pageNumber) { cout &lt;&lt; endl; unsigned char readChar; int iterator = 0, j = 0; char * tempBuffer = (char *)malloc(pageSize); tempBuffer = getPage(pageNumber, fileDescriptor1); for(int i=0;i&lt;pageSize;i++) { readChar = tempBuffer[i]; iterator++; j++; printf("%02x ", readChar);//%02x if (iterator == 16) { iterator = 0; cout&lt;&lt;endl; } } } int main() { readyCache(); char * tempBuffer = getPage(1, fd); int a = 1000; memcpy(tempBuffer, &amp;a, sizeof(int)); setPage(1,tempBuffer, fd); cout&lt;&lt;"\nDump 0\n"; hexdump(fd, 0); cout&lt;&lt;"\nDump 1\n"; hexdump(fd, 1); close(fd); return 0; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload