c++linuxfile

How can I read a file with read() and O_DIRECT in C++ on Linux?


I'm searching for a solution to solve the above described problem.

Here's my "doesn't working code". charsInCurrentBuffer returns always -1!

#define BUFSIZE 512

char *bufferA = new char[BUFSIZE];
char *bufferB = new char[BUFSIZE];

const char *inputFile = "in.txt";

if ( (fdInputFile = open(inputFile, O_DIRECT) ) != -1) {
    cout << "input opened!" << endl;
} else {
    cout << "can't open input file!";
}

int charsInCurrentBuffer = read(fdInputFile, currBuffer, BUFSIZE);
cout << charsInCurrentBuffer << endl;

Solution

  • When you read from an O_DIRECT fd, the "alignment of the user buffer and the file offset must all be multiples of the logical block size of the file system" (quoted from the open man page) on Linux. Other environments might have different constraints on this, and it's in fact filesystem dependent.

    That's not going to be the case with new generally (unless you get lucky).

    You should consider using a posix_memalign function if your platform has that, or simply allocate a larger buffer (BLOCK_SIZE + BUFSIZE) and use the block-size aligned part of it.

    If you want to stick with new, you're going to need to use some form of placement new combined with the above, but I'm not familiar enough with that to show how that would work.

    For reference, see for example this thread on LKML, or the Notes section of the above-quoted man page.