Linux 文件读写
admin
2024-03-01 10:55:21
0

函数的详细介绍可以用man命令来查看,例如 man 2 open.

open

#include int open(const char *pathname, int flags);
int open(const char *pathname, int flags, mode_t mode);

The open() system call opens the file specified by pathname. If the specified file does not exist, it may optionally (if O_CREAT is specified in flags) be created by open().

The return value of open() is a file descriptor, a small, nonnegative integer that is used in subsequent system calls (read(2), write(2), lseek(2), fcntl(2), etc.) to refer to the open file.The file descriptor returned by a successful call will be the lowest-numbered file descriptor not currently open for the process.

lseek

#include 
#include off_t lseek(int fd, off_t offset, int whence);

DESCRIPTION
lseek() repositions the file offset of the open file description associated with the file descriptor fd to the argument offset according to the directive whence as follows:

   SEEK_SETThe file offset is set to offset bytes.SEEK_CURThe file offset is set to its current location plus offset bytes.SEEK_ENDThe file offset is set to the size of the file plus offset bytes.

lseek() allows the file offset to be set beyond the end of the file (but this does not change the size of the file). If data is later written at this point, subsequent reads of the data in the gap (a “hole”) return null bytes (‘\0’) until data is actually written into the gap.

write

#include ssize_t write(int fd, const void *buf, size_t count);

DESCRIPTION
write() writes up to count bytes from the buffer starting at buf to the file referred to by the file descriptor fd.

read

#include ssize_t read(int fd, void *buf, size_t count);

DESCRIPTION
read() attempts to read up to count bytes from file descriptor fd into the buffer starting at buf.

On files that support seeking, the read operation commences at the file offset, and the file offset is incremented by the number of bytes read. If the file offset is at or past the end of file, no bytes are read, and read() returns zero.

示例

#include 
#include 
#include 
#include int main(){int fd;int write_data = 10;int read_data;fd = open("file",O_RDWR | O_CREAT,0644);  //打开文件,若不存在就创建,权限:可读可写+可读+可读lseek(fd,SEEK_SET,0);   //设置写位置write(fd,(char*)&write_data,sizeof(write_data));  //写入数据lseek(fd,SEEK_SET,0);   //设置读位置read(fd,(char*)&read_data,sizeof(read_data));   //读取数据printf("%d\r\n",read_data);return 0;
}

相关内容

热门资讯

安卓系统为什么会赢,技术革新与... 你有没有想过,为什么安卓系统在智能手机市场上如此火爆,几乎成了“手机必备”的存在呢?今天,就让我带你...
电脑可以做安卓系统么,电脑上运... 你有没有想过,电脑能不能装上安卓系统呢?这听起来是不是有点像科幻电影里的情节?别急,让我带你一探究竟...
国产安卓系统碎片化软件,软件生... 你有没有发现,现在手机上的安卓系统越来越丰富多样了?没错,这就是我们今天要聊的话题——国产安卓系统的...
安卓系统的蚂蚁花呗,蚂蚁花呗在... 你知道吗?在安卓系统的世界里,有一个超级方便的支付工具,那就是蚂蚁花呗。它就像你的贴心小助手,让你在...
安卓2系统彩蛋在哪找,揭秘隐藏... 你有没有发现,安卓2系统里竟然隐藏着一些神秘的彩蛋?没错,就是那些让你忍不住想要一探究竟的小惊喜。今...
全球最大的安卓系统,全球最大移... 你知道吗?在智能手机的世界里,有一个系统可是当之无愧的“王者”——那就是安卓系统!它就像一位全能的魔...
安卓系统就没有碎片了,迈向无缝... 你知道吗?最近在科技圈里,安卓系统可是掀起了一阵不小的波澜呢!有人说,安卓系统再也没有碎片化了?这可...
安卓系统平板电脑评测,安卓平板... 你有没有想过,在这个信息爆炸的时代,拥有一台性能卓越的安卓系统平板电脑,简直就是移动办公和娱乐的完美...
双系统安卓自动关机,双系统安卓... 你有没有遇到过这样的情况:手机里装了双系统安卓,一边是工作用的,一边是娱乐用的,结果有时候不小心,手...
圣地安列斯安卓9系统,圣地安列... 亲爱的读者,你是否也像我一样,对科技新动态充满好奇?今天,我要和你分享一个超级有趣的话题——圣地安列...
平果有安卓系统的吗,畅享智能生... 你有没有想过,手机的世界里,竟然还有这样一个有趣的现象?那就是——平果手机,竟然也有安卓系统!是不是...
vivoy27安卓系统下载,畅... 你有没有听说最近Vivo Y27这款手机的新鲜事儿?没错,就是它的安卓系统下载!今天,我就要给你来个...
安卓系统最强定位手机版,最强定... 你有没有想过,在茫茫人海中,如何让你的手机定位功能像侦探一样精准无误?今天,就让我带你一探究竟,揭秘...
安卓运行环境选哪个系统,And... 你有没有想过,你的安卓手机到底是在哪个运行环境下才能发挥出最佳性能呢?这可是个技术活儿,选对了系统,...
zui15系统是安卓系统吗,揭... 亲爱的读者,你是否曾好奇过,那些在手机上运行得风生水起的系统,它们究竟是不是安卓的呢?今天,就让我带...
ios系统和安卓系统权限区别,... 你有没有发现,无论是手机还是平板,我们用的最多的就是那些APP了。而这些APP,它们在手机里可是有着...
荣耀手环6安卓版系统,智能生活... 你有没有注意到,最近你的手腕上是不是多了一抹亮丽的色彩?没错,说的就是荣耀手环6安卓版系统!这款智能...
极品奴隶系统下载安卓版,体验独... 你有没有听说过那个超级火的“极品奴隶系统”安卓版?最近,这款游戏在朋友圈里可是炸开了锅,大家都说它好...
安卓手机苹果系统扣费,揭秘扣费... 你有没有遇到过这种情况?手机里突然多了一笔扣费,而且还是那种你完全没意识到的扣费?尤其是当你用的是安...
安卓系统智能电视刷机,焕新体验 亲爱的电视迷们,你是否曾为你的安卓智能电视的性能所困扰?是不是觉得它运行缓慢,功能受限?别担心,今天...