File I/O programming under Linux
#include <sys> #include <sys> #include int open(const char *pathname, int flags); </sys></sys>
参数1:pathname,文件所在路径
参数2:flags,文件权限,相对于程序进程
常见宏为:O_WRONLY,O_RDONLY,O_RDWR,O_EXCL,O_APPEND,O_DUMP
参数3:mode,当创建文件时候使用,一般为umask的值。
返回值: 成功返回文件描述符,否则返回-1.
#include int close(int fd);
#include ssize_t write(int fd, const void *buf, size_t count);
fd: 文件描述符
buf:存储将要写的数据
count: 写入的长度,以字节为单位
返回值:写入成功时,返回写入的字符长度,否则返回-1。
#include ssize_t read(int fd, void *buf, size_t count);
fd: 文件描述符
buf:存储将要读入的数据
count: 读出的长度,以字节为单位
返回值:读成功时,返回读出的字符长度,否则返回-1。
例如:
#include
#include <sys> #include <unistd.h off_t lseek fd offset int whence> <p>fd: 文件描述符<br> offset:将要偏移 的字节数。<br> whence:从那开始偏移,宏定义如下:<br> SEEK_END 文件末尾<br> SEEK_CUR 当前偏移量位置<br> SEEK_SET 文件开头位置<br> 注意:当偏移量大于文件长度时,产生空洞,空洞是由所设置的偏移量超过文件尾端,并写了一些数据造成了,其从原文件尾端到新偏移的位置之间即是产生的空洞。空洞不占用磁盘空间,可以使用:</p> <pre class="brush:php;toolbar:false">du filename #查看文件所占实际磁盘空间 ls filename #实际文件的大小
例如:
#include #include #include #include <sys> #include <sys> #include #define BUFF 12 int main() { char str1[BUFF] = "jigntikai"; char str2[BUFF] = "wojisuhihawe"; int fd; if ( (fd = open("a.txt",O_WRONLY|O_CREAT,0744)) = { perror("open file fail\n"); exit(EXIT_FAILURE); } if( write(fd,str1,BUFF) == -1 ) { perror("write fial fail\n"); exit(EXIT_FAILURE); } if( lseek(fd,1024,SEEK_END) == -1 ) { perror("lseek fail\n"); } write(fd,str2,BUFF); return 0; } </sys></sys>
#include int access(const char *pathname, int mode);
pathname: 文件名
mode 可以选择以下宏:
F_OK 文件是否存在
R_OK 文件否具有读权限
X_OK 文件否具有可执行权限
W_OK 文件否具有写权限
返回值:满足mode中的参数并且正确执行则返回0 ,否则返回-1。
#include int dup(int oldfd);
oldfd:原来的文件描述符
newfd:指定新的文件描述符数值,如果该描述已经存在则先将其关闭,若oldfd等于newfd,ze返回newfd,而不关闭。
在此简单介绍一下文件的内核结构:首先计算机系统中有一个进程表,其中的每个进程表项,该表项中有一个打开文件描述符表,该打开的文件描述表中有很多文件描述符表项,每项包括两部分:文件描述符标志与文件指针,其中文件指针指向一个文件表,文件表中存放着文件的状态标志即是否可读是否可写,当前文件的偏移量,还有一个v节点指针,指针v节点指针指向一个v节点表,v节点表主要存放文件的所有者,文件长度,文件的设备以及文件实际数据块在磁盘上的位置等一系列信息。可能这样描不太清楚,下面用一张图来描述:
(对于以下会主要是针对的内核缓冲)由于io操作会首先将数据放入内核缓冲区,所以在写的时候如果出现系统故障则缓冲区的数据可能会丢失,所以为了防止这种情况发生,以上两个函数使得内核缓冲区的数据立即写入磁盘。
#include void sync(void);将所有缓冲排入写队列,然后立即返回 int fsync(int fd);将所有缓冲排入写队列,直到该缓冲去的数据写入磁盘后才返回。 int fdatasync(int fd);几乎和fsync函数相同,只是fdatasync(int fd)函数只影响数据部分,而fsync还会同步更新文件的属性。
#include #include int fcntl(int fd, int cmd, ... /* arg */ );
fd:文件描述符
cmd 指明该函数执行什么功能
F_DUPFD 赋值文件描述符,功能相当于dup和dup2函数。例如:
dup(fd)等价于
fcntl(fd,F_DUPFD,0)
dup2(oldfd,newfd)等价于
close(newfd);
fcntl(oldfd,F_DUPFD,newfd);
F_GETFD 的到文件描述符标志,当前之定义一个文件描述符标志,FD_CLOSEEXEC.此时第三个参数被忽视。
F_SETFD 设置文件描述符标志,设置的值是函数的第三个参数,其一般可设置为0表示关闭,1表示打开。
F_SETFL 设置文件状态标志,其值放在函数的第三个参数,和open函数第二个参数的值一样的。
F_GETFL 得到文件状态标志。此时第三个参数被忽视。
arg 可选参数,根据第二个参数填写。
返回值:出错返回-1,否则哈返达到的标志。
例如:
#include #include #include #include int main(int argc,char * argv[]) { int fd; int val=3; if( (fd = open(argv[1],O_RDWR|O_APPEND)) == -1 )//测试一下是否可以同时检测出文件的读写属性 { exit(2); } if( val = fcntl(fd,F_GETFL,0) == -1 ) { exit(1); } printf("%d\n",val); printf("%d %d %d\n",O_RDONLY,O_WRONLY,O_RDWR); int n = val & O_ACCMODE; if( n == O_RDONLY) printf("read\n"); if(O_WRONLY & val ) printf("write\n"); if( n == O_RDWR) printf("read and write\n"); }
linux中有两个级别的缓冲:IO缓冲与内核缓冲
(1)IO缓冲:对于标准IO操作,都会有一个缓冲区,当用户想要写数据时,首先将数据写入缓冲区,待缓冲区满之后才能调用系统函数写入内核缓冲区。当用户想读取数据时,首先向内核读取一定的数据放入IO缓冲区,读操作从缓冲区中读数据,当读完IO缓冲区的数据时,才能再读取数据到IO缓冲区。
目的:减少对磁盘的读写次数,提高工作效率。
(2)内核缓冲区:操作系统内核部分也有缓冲,其与IO缓冲区是不同的,其主要区别用一张图表示:
The above is the detailed content of File I/O programming under Linux. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



The key differences between CentOS and Ubuntu are: origin (CentOS originates from Red Hat, for enterprises; Ubuntu originates from Debian, for individuals), package management (CentOS uses yum, focusing on stability; Ubuntu uses apt, for high update frequency), support cycle (CentOS provides 10 years of support, Ubuntu provides 5 years of LTS support), community support (CentOS focuses on stability, Ubuntu provides a wide range of tutorials and documents), uses (CentOS is biased towards servers, Ubuntu is suitable for servers and desktops), other differences include installation simplicity (CentOS is thin)

CentOS will be shut down in 2024 because its upstream distribution, RHEL 8, has been shut down. This shutdown will affect the CentOS 8 system, preventing it from continuing to receive updates. Users should plan for migration, and recommended options include CentOS Stream, AlmaLinux, and Rocky Linux to keep the system safe and stable.

Docker uses Linux kernel features to provide an efficient and isolated application running environment. Its working principle is as follows: 1. The mirror is used as a read-only template, which contains everything you need to run the application; 2. The Union File System (UnionFS) stacks multiple file systems, only storing the differences, saving space and speeding up; 3. The daemon manages the mirrors and containers, and the client uses them for interaction; 4. Namespaces and cgroups implement container isolation and resource limitations; 5. Multiple network modes support container interconnection. Only by understanding these core concepts can you better utilize Docker.

CentOS installation steps: Download the ISO image and burn bootable media; boot and select the installation source; select the language and keyboard layout; configure the network; partition the hard disk; set the system clock; create the root user; select the software package; start the installation; restart and boot from the hard disk after the installation is completed.

How to use Docker Desktop? Docker Desktop is a tool for running Docker containers on local machines. The steps to use include: 1. Install Docker Desktop; 2. Start Docker Desktop; 3. Create Docker image (using Dockerfile); 4. Build Docker image (using docker build); 5. Run Docker container (using docker run).

Backup and Recovery Policy of GitLab under CentOS System In order to ensure data security and recoverability, GitLab on CentOS provides a variety of backup methods. This article will introduce several common backup methods, configuration parameters and recovery processes in detail to help you establish a complete GitLab backup and recovery strategy. 1. Manual backup Use the gitlab-rakegitlab:backup:create command to execute manual backup. This command backs up key information such as GitLab repository, database, users, user groups, keys, and permissions. The default backup file is stored in the /var/opt/gitlab/backups directory. You can modify /etc/gitlab

CentOS hard disk mount is divided into the following steps: determine the hard disk device name (/dev/sdX); create a mount point (it is recommended to use /mnt/newdisk); execute the mount command (mount /dev/sdX1 /mnt/newdisk); edit the /etc/fstab file to add a permanent mount configuration; use the umount command to uninstall the device to ensure that no process uses the device.

After CentOS is stopped, users can take the following measures to deal with it: Select a compatible distribution: such as AlmaLinux, Rocky Linux, and CentOS Stream. Migrate to commercial distributions: such as Red Hat Enterprise Linux, Oracle Linux. Upgrade to CentOS 9 Stream: Rolling distribution, providing the latest technology. Select other Linux distributions: such as Ubuntu, Debian. Evaluate other options such as containers, virtual machines, or cloud platforms.
