Linux文件操作

用代码操作文件,实现文件创建,打开,编辑等自动化执行

Linux系统API
打开 open
读写 write/read
光标定位 lseek
关闭 close

1.创建文件

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

   int creat(const char *pathname, mode_t mode);**
		pathname:要创建的文件名(包括路径,缺省为当前路径)
		mode:创建模式
		常见创建模式:
		宏表示        数字
		S_IRUSR       4             可读
		S_IWUSR       2             可写
		S_IXUSR       1             可执行
		S_IRWXU       7             可读可写可执行
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int main()
{
        int fd;

//      int creat(const char *pathname, mode_t mode);
        fd = creat("./file4",S_IRWXU);

        return 0;
}
~    

2.打开文件

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);

Pathname :要打开的文件名(含路径,缺省为当前路径)
Flags:
O_RDONLY 只读打开
O_WRONLY 只写打开
O_RDWR 可读可写打开
当我们附带了权限后,打开的文件就只能按照这种权限来操作。
以上这三个常数中应当只指定一个。下列常数是可选择的
O_CREAT 若文件不存在则创建它。使用此选项时,需要同时说明第三个参数mode.用其说明该新文件的存取许可权限。
O_EXCL 如果同时指定了OCREAT,而文件己经存在,则出错。
O_APPEND 每次写时都加到文件的尾端
O_TRUNC 属性去打开文件时,如果这个文件中本来是有内容的,而且为只读或只写成功打开,则将其长度截短为0。

Mode:一定是在flags中使用了O_CREAT标志,mode记录待创建的文件的访问权限
文件权限:
可读 r 4
可写 w 2
可执行 x 1
例如:0600->可读可写

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

int main()
{
        int fd;

        fd = open("./file1",O_RDWR);

        if(fd = -1){
                printf("open file failed!\n");
                fd = open("./file1",O_RDWR|O_CREAT,0600);
                if(fd>0){
                        printf("creat file1 success\n");
                }
        }

        return 0;
}

3.写入文件

SYNOPSIS
*#include <unistd.h>
ssize_t write(int fd, const void buf, size_t count);

DESCRIPTION
最多写入以计数字节数count,从缓冲区指向 buf 到文件描述符 fd 引用的文件。

RETURN VALUE
成功时,将返回写入的字节数(零表示什么也没写)。 出错时,返回 -1。

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>

int main()
{
        int fd;
        char *buf = "Hello world";

        fd = open("./file1",O_RDWR);

        if(fd = -1){
                printf("open file failed!\n");
                fd = open("./file1",O_RDWR|O_CREAT,0600);
                if(fd>0){
                        printf("creat file1 success\n");
                }
        }

        printf("open success:fd = %d\n",fd);

//      ssize_t write(int fd, const void *buf, size_t count);
        write(fd,buf,strlen(buf));
						//计算缓冲区大小时不能用sizeof(),要用strlen。strlen()函数用来计算字符串的长度而sizeof () 是一个判断数据类型或者表达式长度的运算符。
        close(fd);

        return 0;

4.读取文件

SYNOPSIS
*#include <unistd.h>
ssize_t read(int fd, void buf, size_t count);

DESCRIPTION
read() 尝试读取以将文件描述符 fd 中的字节数计算到缓冲区从 buf 开始。
如果 count 为零,则 read() 返回零并且没有其他结果。 如果计数大于 SSIZE_MAX,结果未指定。

RETURN VALUE
成功时,将返回读取的字节数(零表示结束file),并且文件位置按此数字前进。 出错时,返回 -1,并设置 errno。

ERRORS
EAGAIN :文件描述符 fd 引用套接字以外的文件,并且已被标记为非阻塞(O_NONBLOCK),读取将阻塞。

5.光标定位

SYNOPSIS
#include <sys/types.h>
#include <unistd.h>

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

DESCRIPTION
lseek() 函数根据指令将与文件描述符 fd 关联的打开文件的偏移量重新定位为参数偏移量,如下所示:

   SEEK_SET:
          偏移量设置为偏移字节。

   SEEK_CUR:
          偏移量设置为其当前位置加上偏移字节。

   SEEK_END:
         偏移量设置为文件大小加上偏移字节。

RETURN VALUE
成功完成后,lseek() 将返回从文件开头开始以字节为单位测量的结果偏移位置(可用于计算文件大小)。 出错时,返回值 (off_t) -1,并设置 errno 以指示错误。

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

int main()
{
        int fd;
        char *buf = "Hello world!";

        fd = open("./file1",O_RDWR);

        if(fd = -1){
                printf("open file failed!\n");
                fd = open("./file1",O_RDWR|O_CREAT,0600);
                if(fd>0){
                        printf("creat file1 success\n");
       c         }
        }

        printf("open success:fd = %d\n",fd);

//      ssize_t write(int fd, const void *buf, size_t count);
        int n_write = write(fd,buf,strlen(buf));
                        //计算缓冲区大小时不能用sizeof(),要用strlen()a。
        if(n_write != -1){
                printf("write %d byte to file1\n",n_write);
        }

/*      读文件前一定要先解决光标问题,因为在写文件时光标会移动到文件末尾,如果不将光标移动到文件首部,读取到的文件将是空。
        方法一:重新关闭打开文件,再读取文件。(很low!!!)
        方法二:将光标移动到文件头部。
*/
/*      方法一如下:
        close(fd);
        fd = open("./file1",O_RDWR);
*/
//      方法二如下:
        lseek(fd,0,SEEK_SET);
        
        char *readBuf;
        readBuf = (char *)malloc(sizeof(char)*n_write+1);
        int n_read = read(fd,readBuf,n_write);
        printf("read %d byte,context:%s\n",n_read,readBuf);

        close(fd);

        return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值