linux 驱动第4课 - 文件io

 

https://ke.qq.com/course/466167?taid=4166603608890615

1:  ls -al /dev 下面显示的第一个字母如果是c 代表的是字符设备,b 代表的是块设备

2: 后面的 信息里面,第一个是主设备好,第二个是次设备号

root@iZrj9e3565o1e423gw4xgiZ:~# ls -al /dev
total 4
drwxr-xr-x 18 root root        3740 Jun 24 02:31 .
drwxr-xr-x 25 root root        4096 Jun 24 02:53 ..
crw-r--r--  1 root root     10, 235 Jun 24 02:31 autofs
drwxr-xr-x  2 root root         240 Jun 24 10:31 block
crw-------  1 root root     10, 234 Jun 24 02:31 btrfs-control
drwxr-xr-x  3 root root          60 Jun 24 02:30 bus
drwxr-xr-x  2 root root        3240 Jun 24 02:31 char
crw-------  1 root root      5,   1 Jun 24 02:31 console
lrwxrwxrwx  1 root root          11 Jun 24 10:31 core -> /proc/kcore
crw-------  1 root root     10,  60 Jun 24 02:31 cpu_dma_latency
crw-------  1 root root     10, 203 Jun 24 02:31 cuse
drwxr-xr-x  7 root root         140 Jun 24 10:31 disk
drwxr-xr-x  3 root root          80 Jun 24 02:31 dri
crw-------  1 root root     10,  62 Jun 24 02:31 ecryptfs
crw-rw----  1 root video    29,   0 Jun 24 02:31 fb0
lrwxrwxrwx  1 root root          13 Jun 24 10:31 fd -> /proc/self/fd
crw-rw-rw-  1 root root      1,   7 Jun 24 02:31 full
crw-rw-rw-  1 root root     10, 229 Jun 24 02:31 fuse
crw-------  1 root root    244,   0 Jun 24 02:31 hidraw0
crw-------  1 root root     10, 228 Jun 24 02:31 hpet
drwxr-xr-x  2 root root           0 Jun 24 02:31 hugepages
crw-------  1 root root     10, 183 Jun 24 02:31 hwrng
crw-------  1 root root     89,   0 Jun 24 02:31 i2c-0

3:  查用第二个

查看系统调用 使用man 2 open

    Executable programs or shell commands      // 命令   
	System calls (functions provided by the kernel)  // 系统调用,比如 man 2 open   
	Library calls (functions within program libraries)  // 函数库调用   
	Special files (usually found in /dev)          // 特殊文件, 比如 man 4 tty    
	File formats and conventions eg /etc/passwd  // 文件格式和约定, 比如man 5 passwd   
	Games  // 游戏   
	Miscellaneous (including macro packages and conventions), e.g. man(7), groff(7) //杂项   
	System administration commands (usually only for root) // 系统管理命令   
	Kernel routines [Non standard]  // 内核例程   
OPEN(2)                                                               Linux Programmer's Manual                                                              OPEN(2)

NAME
       open, openat, creat - open and possibly create a file

SYNOPSIS
       #include <sys/types.h>
       #include <sys/stat.h>
       #include <fcntl.h>

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

       int creat(const char *pathname, mode_t mode);

       int openat(int dirfd, const char *pathname, int flags);
       int openat(int dirfd, const char *pathname, int flags, mode_t mode);

   Feature Test Macro Requirements for glibc (see feature_test_macros(7)):

       openat():
           Since glibc 2.10:
               _POSIX_C_SOURCE >= 200809L
           Before glibc 2.10:
               _ATFILE_SOURCE

4: 

有时间可以打一下 下面的代码:

#include <sys/types.h>   
	#include <sys/stat.h>   
	#include <fcntl.h>   
	#include <unistd.h>   
	#include <stdio.h>   
	#include <sys/mman.h>   
   
	/*   
		* ./copy 1.txt 2.txt   
		* argc   = 3   
		* argv[0] = "./copy"   
		* argv[1] = "1.txt"   
		* argv[2] = "2.txt"   
		*/   
	int main(int argc, char **argv)   
	{   
			int fd_old, fd_new;   
			struct stat stat;   
			char *buf;   
   
			/* 1. 判断参数 */   
			if (argc != 3)   
			{   
					printf("Usage: %s <old-file> <new-file>\n", argv[0]);   
					return -1;   
			}   
   
			/* 2. 打开老文件 */   
			fd_old = open(argv[1], O_RDONLY);   
			if (fd_old == -1)   
			{   
					printf("can not open file %s\n", argv[1]);   
					return -1;   
			}   
   
			/* 3. 确定老文件的大小 */   
			if (fstat(fd_old, &stat) == -1)   
			{   
					printf("can not get stat of file %s\n", argv[1]);   
					return -1;   
			}   
   
			/* 4. 映射老文件 */   
			buf = mmap(NULL, stat.st_size, PROT_READ, MAP_SHARED, fd_old, 0);   
			if (buf == MAP_FAILED)   
			{   
					printf("can not mmap file %s\n", argv[1]);   
					return -1;   
			}   
   
			/* 5. 创建新文件 */   
			fd_new = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);   
			if (fd_new == -1)   
			{   
					printf("can not creat file %s\n", argv[2]);   
					return -1;   
			}   
   
			/* 6. 写新文件 */   
			if (write(fd_new, buf, stat.st_size) != stat.st_size)   
			{   
					printf("can not write %s\n", argv[2]);   
					return -1;   
			}   
   
			/* 5. 关闭文件 */   
			close(fd_old);   
			close(fd_new);   
   
			return 0;   
	}   

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值