Linux中,一切皆文件(网络设备除外)。 硬件设备也“是”文件,通过文件来使用设备。 目录(文件夹)也是一种文件。
fd是一个大于等于0的整数。 每打开一个文件,就创建一个文件描述符,通过文件描述符来操作文件。
预定义的文件描述符: 0: 标准输入,对应于已打开的标准输入设备(键盘) 1: 标准输出,对应于已打开的标准输出设备(控制台) 2: 标准错误,对应于已打开的标准错误输出设备(控制台) (运行程序在proc文件夹中的对应进程文件夹下查看fd文件夹) 多次打开同一个文件,可以得到多个不同的文件描述符。
例如:read
例如: fread
(关于文件的系统调用)
往一个文件描述符中写数据。
返回值:
注意:
示例:
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
int main(void) {
char buff[] = "hello world\n";
//1 2 都是输出到控制台
int len = 0;
len = write(1, buff, sizeof(buff));//标准输出
if (len < 0) {
printf("write failed.reason:%s\n",strerror(errno));
}
len = write(2, buff, sizeof(buff));//标准出错输出
if (len < 0) {
printf("write failed.reason:%s\n", strerror(errno));
}
return 0;
}
从一个文件描述符中读取count个字节到buff中。
返回值:
注意:
示例:
char buffer[1024];
int cnt = read(0,buffer,sizeof(buffer));//从标准输入读
int open(const char *pathname, int flags);
int open(const char *pathname, int flags, mode_t mode);
打开文件
返回值:
打开方式:
时,则返回错误, 用途:以防止多个进程同时创建 同一个文件
参数3:设置权限
注意:
示例:
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#define FILE_RW_LEN 1024
int main(void) {
//第二个参数-文件存在则无法打开
//O_AOOEND —— 追加
int fd = open("./test_open.txt", O_CREAT | O_EXCL | O_RDWR,S_IRWXU | S_IRGRP |S_IXGRP | S_IROTH);
int count = 0;
char buffer[FILE_RW_LEN] = "hello i am test";
if (fd < 0) {
printf("open failed!,reason :%s\n",strerror(errno));
exit(-1);
}
count = write(fd, buffer, strlen(buffer));
printf("written: %d bytes\n",count);
close(fd);
return 0;
}
提示:!gcc——重新执行gcc上次编译的命令
int close(int fd);
关闭文件
终止指定文件描述符与对应文件之间的关联。 并释放该文件描述符,即该文件描述符可被重新使用。
返回值:
示例:
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#define FILE1_NAME "file1.txt"
#define FILE2_NAME "file2.txt"
int main(void) {
int file1, file2;
char buffer[4096];
int len = 0;
file1 = open(FILE1_NAME, O_RDONLY);
if (file1 < 0) {
printf("open file failed! reason:%s\n",strerror(errno));
exit(-1);
}
file2 = open(FILE2_NAME, O_CREAT | O_WRONLY ,S_IRUSR | S_IWUSR);
if (file2 < 0) {
printf("open file failed! reason:%s\n", strerror(errno));
exit(-2);
}
while (len = read(file1, buffer, sizeof(buffer) > 0)) {
write(file2,buffer,len);
}
close(file2);//实战记得判断是否关闭成功
close(file1);
return 0;
}
提示:
off_t lseek(int fd, off_t offset, int whence);
重新定义读写文件的偏移。
返回值:
**示例:**从一个文件偏移100处,拷贝100字节到另一个文件。
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#define FILE1_NAME "lseek_test.c"
#define FILE2_NAME "lseek_test2.txt"
#define SIZE 100
int main(void) {
int file1, file2;
char buffer[1024];
int ret;
file1 = open(FILE1_NAME, O_RDONLY);
if (file1 < 0) {
printf("open file failed! reason:%s\n", strerror(errno));
exit(-1);
}
file2 = open(FILE2_NAME, O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR);
if (file2 < 0) {
printf("open file failed! reason:%s\n", strerror(errno));
exit(-2);
}
//文件句柄,偏移量,从哪偏移
ret = lseek(file1, 0, SEEK_END);
printf("file size: %d\n", ret);
ret = lseek(file1, 100, SEEK_SET);
printf("lseek ret:%d\n", ret);
ret = read(file1, buffer, SIZE);
if (ret > 0) {
buffer[ret] = '\0';
write(file2,buffer,SIZE);
}
close(file1);
close(file2);
return 0;
}
嵌入式相关
ioctl是设备驱动程序中对设备的I/O通道进行管理的函数。所谓对I/O通道进行管理,就是对设备的一些特性接口进行控制,例如串口的传输波特率、马达的转速等等。是设备驱动程序中设备控制接口函数,用来控制设备。
int ioctl(int fd, int cmd,[int *argdx, int argcx]);
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有