0

I am trying to redesign the API of my previous project that I've worked on it, which was written in C, in a way that it can be used in an application written in C++ language.

I was using in C language lseek() for chasing table, pages etc. My question is, Is it okay to still use lseek() in C++ the way that I have used it, like a sample code below:

/* Read page data from an offset. It assumes that pagenum is zero-indexed*/
lseek(bq.unixfd, PAGE_SIZE + (PAGE_SIZE * bq.pagenum), SEEK_SET);

thanks alot

N007
  • 79
  • 10
  • Well, in modern C++ you'd probably use `std::ifstream`, as it's cross-platform. But you might have specific needs that doesn't provide though, so an OS-provided API might be the best choice. – tambre Jun 26 '17 at 12:56
  • 1
    the middle argument which is the offset, i've often had problems with it being a 32-bit value and failing to work on files larger than 4gb. so i would look into `lseek64`. Whether there's something better than lseek for c++ i don't know. – ron Jun 26 '17 at 12:59

1 Answers1

1

The C++ standard explicitely allows to use C standard library in C++:

17.2 The C standard library [library.c]
The C++ standard library also makes available the facilities of the C standard library, suitably adjusted to ensure static type safety.

Here the lseek function is not defined by the C standard but by the POSIX.1 one. Anyway on systems that support it, it is included to the standard C library and made available to C++ program exactly the same as standard C functions.

That being said, without knowing more of your requirements (portability, performances, use cases, etc.), I cannot say whether it is a nice idea to use low level IO functions or whether it would be better to switch to C++ streams.

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252