LINUX 文件编程

1.文件的打开与创建

总述:我们在LINUX上打开文件时,在一个进程里打开多个文件时,需要依靠文件描述符号进行区分,它返回的时一个int 类型的数据。
下面展示一些 文件打开的参数说明。

// 参数说明
int open(const char*pathname,int flags);
int open(const char*pathname,int flags,mode_t mode);

其中的pathname为文件名字,flags 为权限:
O_RDONLY  只读打开
O_WRONLY 只写打开
O_RDWR     可读可写
mode 是在创建文件的时候flags中使用了O_CREAT,mode记录带创建文件的权限

// 我们可以在LINUX下还可以看到我们文件的各个情况
运用的命令: ls -l
以我的Linux为例子
-rwxr-xr-x 1 LS book  8426 Feb  2 13:46 a.out
-rw-r--r-- 1 LS book  3508 Feb  2 00:58 curses.c
-rw-r--r-- 1 LS book   114 Dec 25 13:01 DongJJ.c
-rw------- 1 LS book     0 Feb  2 13:46 File
-rw-r--r-- 1 LS book   270 Feb  2 13:46 file.c
-rwxr-xr-x 1 LS book  8381 Dec 24 19:20 FirstProgram
-rw-r--r-- 1 LS book  3704 Jan 28 11:51 link1.c
-rw-r--r-- 1 LS book    21 Nov  1  2018 readMe
-rwxr-xr-x 1 LS book 13514 Feb  2 00:58 sanake
-rwxr-xr-x 1 LS book  8516 Jan 29 23:34 sanke
-rwxr-xr-x 1 LS book  8373 Dec 24 19:23 secondprogram
-rwxr-xr-x 1 LS book  8436 Dec 25 13:04 thirthprogram
我们可以在上面看到各种操作时间和权限,rwxr指的是其中
r指的是读的权限(4);w指的是写的权限(2);x指的是可执行权限(1)
其中还有一点可以说的是我再写File.c的时候在mode参数上写的是0600,6指的是4+2即刻可读可写,Linux和我们在VS上写的程序不太一样就是我们的文件和可执行文件是两个东西。

2.文件的读取

下面展示代码。

// A code block
#include<sys/types.h>
#include<sys/stat.h>
#include<stdio.h>
#include<fcntl.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>

int main(){

        int fd;
        char*buff = "hello i am file";
        fd = open("./File",O_RDWR);
        if(fd==-1){printf("open fialed\n");}

        fd = open("./File",O_RDWR|O_CREAT,0600);

        if(fd>0){printf("open sucess\n");}

        int n_write = write(fd,buff,strlen(buff));

        printf("write %d bytes into wenjian\n",n_write);

        char*strbuff = (char *)malloc(sizeof(char)*n_write+1);

        int n_read = read(fd,strbuff,n_write);

        printf("n_read = %d,strbuff: %s\n",n_read,strbuff);

        close(fd);

        return 0;


运行结果
程序运行的结果

查看文件读取的函数声明,第一个参数还是fd文件标志,第二个参数是我们定义的缓存内存,第三个文件字节数的大小

函数读取成功是返回一个读取的字节数,并且内容存储在缓存内存中,不成功返回-1。但是我们正常读取的时候文件标志位是在文件的末尾所以我们需要运用函数改变标志位。

1.whence是决定光标位置,第一个SEEK_SET是指的是文头,SEEK_CUR是指的文件当前光标,SEEK_END指的是文件尾。
2.offset 指的是相对于whence的偏移量。(负数表示向前偏移,正数向后偏移
3.fd就不说了。
通过上面的程序运行看到并没有读取出来,是因为光标一直在文件末尾,读取的是空的内容和字节数。我们相对于上面只需加上:

// A code block
lseek(fd,0,SEEK_SET);//相对于文件头偏移量是0,即把光标放在文件头
//记录这个函数的返回值是一个偏移量,我们也可以用来计算文本的字节数
int n_read = read(fd,strbuff,n_write);

在这里插入图片描述
读取成功

3.对于上述内容的练习

(一)文件操作之实现cp功能的代码

直接上代码。

// A code block
#include<stdlib.h>
#include<unistd.h>
#include<string.h>

int main(int argc,char**argv)
{
        int fd1;
        int fd2;
        fd1 = open(argv[1],O_RDONLY);//参数一和只读权限
        if(fd1==-1){printf("open fialed\n");}//判断

        int n = lseek(fd1,0,SEEK_END);//获取字节大小

        char *buff = (char *)malloc(sizeof(char)*n+1);//malloc空间

        lseek(fd1,0,SEEK_SET);//移动光标到开头

        read(fd1,buff,n);//读文件

        fd2 = open(argv[2],O_RDWR|O_CREAT,0600);//打开文件
        
        int n_write = write(fd2,buff,n);//写入文件

        close(fd1);//关闭文件
        close(fd2);//关闭文件
        return 0;
}
//代码文档是后面编辑加上的 Linux里全英文               

实现思路:
1.是关于main函数的传参问题
linux main函数参数讲解
2.是设计思路
a.先对我们所编辑的文件进行读取
b.传入一个临时的指针
c.打开另外一个文件
d.从缓存区里面写入文件
e.关闭文件

// A code block
LS@Embed_Learn:~$ 
LS@Embed_Learn:~$ ./mycp file.c file1.c
LS@Embed_Learn:~$ 
LS@Embed_Learn:~$ 
LS@Embed_Learn:~$ 
LS@Embed_Learn:~$ ls
a.out     DongJJ.c  file1.c  FirstProgram  mycp    sanake  secondprogram  thirthprogram
curses.c  File      file.c   link1.c       readMe  sanke   
LS@Embed_Learn:~$ 
LS@Embed_Learn:~$ 

运行成功

以上是本人学习记录,水平有限,如有错误,欢迎指正!!!over。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值