简体   繁体   English

C和C ++(lseek())之间的区别

[英]DIfference between C and C++ ( lseek() )

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. 我正在尝试重新设计我以前使用C编写的项目的API,以便可以在以C ++语言编写的应用程序中使用它。

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: 我使用C语言的lseek()来跟踪表,页面等。我的问题是,仍然可以以我使用它的方式在C ++中仍然使用lseek() ,例如下面的示例代码:

/* 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 非常感谢

The C++ standard explicitely allows to use C standard library in C++: C ++标准明确允许在C ++中使用C标准库:

17.2 The C standard library [library.c] 17.2 C标准库[library.c]
The C++ standard library also makes available the facilities of the C standard library, suitably adjusted to ensure static type safety. C ++标准库还提供了C标准库的功能,并对其进行了适当调整以确保静态类型安全。

Here the lseek function is not defined by the C standard but by the POSIX.1 one. 这里的lseek函数不是由C标准定义的,而是由POSIX.1定义的。 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. 无论如何,在支持它的系统上,它都包含在标准C库中,并且可以与标准 C函数完全相同地提供给C ++程序使用。

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. 就是说,在不了解您的更多需求(可移植性,性能,用例等)的情况下,我不能说使用低级IO功能是个好主意还是切换到C ++流是否更好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM