Linux文件编程

目录

1.open—文件的创建与打开

2.write—文件写入操作

3.read—文件读取操作

4.lseek—文件光标移动操作

5.fopen,fread,fwrite,fseek,fclose函数


1.open—文件的创建与打开

函数原型

SYNOPSIS
       #include <sys/types.h> //这里提供类型pid_t和size_t的定义
       #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);  //创建文件
Pathname文件的路径名,如果只写文件名,就默认当前目录,如果在文件名加上路径,就按照绝对路径来打开文件
FlagsO_RDONLY  只读打开         O_WRONLY  只写打开         O_RDWR  可读可写打开
mode一定是在flags中使用了O_CREAT标志,mode记录待创建的文件的访问权限

文件打开成功返回文件描述符,失败返回-1


在Flags参数中还可以加入以下参数     

        O_CREAT 若文件不存在则创建它。使用此选项时,需要同时说明第三个参数mode,用其说明该新文件的存取许可权限。

        O_EXCL 如果同时指定了OCREAT,而文件已经存在,则出错。       

        O_APPEND 每次写时都加到文件的尾端。

        O_TRUNC 属性去打开文件时,如果这个文件中本来是有内容的,而且为只读或只写成功打开,则将其长度截短为0。


Linux文件权限

Linux 系统中采用三位十进制数表示权限,如0755, 0644.

7 1+2+4

5 1+4

5 1+4

ABCD

·A- 0, 表示十进制

·B-用户

·C-组用户

·D-其他用户

‐‐‐ ‐> 0 (no excute , nowrite ,no read)
‐‐x ‐> 1 excute, (nowrite, no read)
‐w‐ ‐> 2 write
r‐‐ ‐> 4 read
‐wx ‐> 3 write, excute
r‐x ‐> 5 read, excute
rw‐ ‐> 6 read, write 
read, write , excute

demo

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

int main()
{
     int fd;
     fd = open("./file",O_RDWR);

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

2.write—文件写入操作

函数原型

SYNOPSIS
       #include <unistd.h>
       ssize_t write(int fd, const void *buf, size_t count);
fd文件描述符
*buf读入数据的首地址
count写入数据个数

如果顺利write()会返回实际写入的字节数(len)。当有错误发生时则返回-1,错误代码存入errno中


文件关闭操作 

SYNOPSIS
       #include <unistd.h>
       int close(int fd);

功能就是简单的关闭文件,在每次写完之后都要调用close函数关闭文件

demo

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

 int  main()
{
   int fd;  //定义文件描述符
   char *buf="liujiaxin"; //定义写入内容
   fd = open("./file1", O_RDWR);
   if(fd=-1){
   printf("open file fail\n");
   fd = open("./file1", O_RDWR|O_CREAT,0600);
     if(fd>0){
      printf("open file success\n");
     }
    }
   printf("open fail success:%d/n",fd);
    close(fd);
   return 0;
}

3.read—文件读取操作

函数原型

SYNOPSIS
       #include <unistd.h>
       ssize_t read(int fd, void *buf, size_t count);
fd文件描述符
*buf写入的数据的首地址
count写入数据个数

成功返回读取的字节数,出错返回-1并设置errno,如果在调read之前已到达文件末尾,则这次read 返回0

在写入完内容后读取时要注意光标移动的问题,在写入完内容后光标移动到了内容的结尾,如果不把光标挪到内容开头的话是说明也读不到的。光标移动到开头有两种方式,关闭文件紧接着再打开或者就是调用lseek函数。

demo

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

int main()
{
     int fd;
     fd = open("./file",O_RDWR);
     char *buf = "sdad";

     //打开文件
     if(fd == -1){
        printf("open file fail\n");
        fd = open("./file",O_RDWR|O_CREAT,0600);
        if(fd > 0){
                printf("creat file success\n");
        }
     }
     printf("open file success\n");
     //写入内容
     int n_write = write(fd,buf,strlen(buf));
     if(n_write != -1){
        printf("write success\n");
     }close(fd);

     open("./file",O_RDWR);//重新打开文件
     //读取内容
     char *BufRead;
     BufRead = (char *)malloc(sizeof(char)*n_write+1);
     int n_read = read(fd,BufRead,n_write);
     printf("read %d bytes,context is %s\n",n_read,BufRead);

     close(fd);
     return 0;
}

4.lseek—文件光标移动操作

函数原型

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

       off_t lseek(int fd, off_t offset, int whence);
fd文件描述符
offset对whence的偏移值
whence

偏移到的位置(Lniux系统种提供三个宏定义)

1.SEEK_SET      偏移到头

2.SEEK_END     偏移到尾

3.SEEK_CRU     偏移到当前位置

调用成功返回偏值,失败返回-1。

demo

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

int main()
{
     int fd;
     fd = open("./file",O_RDWR);
     char *buf = "sdad";

     //打开文件
     if(fd == -1){
        printf("open file fail\n");
        fd = open("./file",O_RDWR|O_CREAT,0600);
        if(fd > 0){
                printf("creat file success\n");
        }
     }
     printf("open file success\n");
     //写入内容
     int n_write = write(fd,buf,strlen(buf));
     if(n_write != -1){
        printf("write success\n");
     }

      //使光标偏移到文件开头
      lseek(fd,0,SEEK_SET); 

     //读取内容
     char *BufRead;
     BufRead = (char *)malloc(sizeof(char)*n_write+1);
     int n_read = read(fd,BufRead,n_write);
     printf("read %d bytes,context is %s\n",n_read,BufRead);

     close(fd);
     return 0;
}

5.fopen,fread,fwrite,fseek,fclose函数

fopen与open的区别

open是UNIX/LINUX系统调用的函数,返回的是文件描述符,它是文件在文件描述符里的索引。

fopen是标准C语言库中函数,在不同系统中应调用不同的内核api,返回值是应该指向文件结构的指针。

移植性

fopen是标准C语言库中函数,移植性更强;open属于UNIX/LINUX系统,移植性有限。

 fopen 在用户态是缓存的,open 在用户态是没有缓存的。


fopen函数原型

FILE * fopen(constchar *path , cost char *mode)
/*
* @description : 打开一个文件
* @param ‐ path : 指定文件路径,如:"./test.txt"
* @param ‐ mode :指定文件的打开方式,如下图:
* @return : 成功,返回指向该文件的文件指针; 若失败,返回 NULL
*/

参数说明:第一个参数为欲打开文件的文件路径及文件名,第二个参数表示对文件的打开方式

注:mode有以下值:

r:只读方式打开,文件必须存在
r+:可读写,文件必须存在
rb+:打开二进制文件,可以读写
rt+:打开文本文件,可读写
w:只写,文件存在则文件长度清0,文件不存在则建立该文件
w+:可读写,文件存在则文件长度清0,文件不存在则建立该文件
a:附加方式打开只写,不存在建立该文件,存在写入的数据加到文件尾,EOF符保留
a+:附加方式打开可读写,不存在建立该文件,存在写入的数据加到文件尾,EOF符不保留
wb:打开二进制文件,只写 
wb+:打开或建立二进制文件,可读写 wt+:打开或建立文本文件,可读写
at+:打开文本文件,可读写,写的数据加在文本末尾
ab+:打开二进制文件,可读写,写的数据加在文件末尾

fread函数原型

size_t fread(void*buff , size_t size, size_t count , FILE* stream)

/*参数说明:
第一个参数为接收数据的指针(buff),也即数据存储的地址
第二个参数为单个元素的大小,即由指针写入地址的数据大小,注意单位是字节
第三个参数为元素个数,即要读取的数据大小为size的元素个素
第四个参数为提供数据的文件指针,该指针指向文件内部数据
返回值:读取的总数据元素个数*/

fread()作用:从fp所指向文件的当前位置开始,一次读入size个字节,重复count次,并将读入的数据存放到从buffer开始的内存中; buffer是存放读入数据的起始地址(即存放何处)。 


fwrite函数原型

int fwrite(void*buffer,int size,int count,FILE*fp)

fwrite():从buffer开始,一次输出size个字节,重复count次,并将输出的数据存放到fp所指向的文件 中。buffer是要输出数据在内存中的起始地址(即从何处开始输出)。


fseek函数原型

int fseek(FILE *stream,long offset,int framewhere)

/*framewhere分别用3个宏:
SEEK_SET 既0 文件开头
SEEK_CUR 既1 文件当前位置
SEEK_END 既2 文件结尾*/

参数:第一个为文件指针,第二个是指针的偏移量,第三个是指针偏移起始位置

返回值:重定位成功返回0,否则返回非零值


fclose函数原型

int fclose(FILE*stream)
/*
* @description :关闭一个已打开的流
* @param ‐ stream :文件指针(流)
* @return : 成功,返回0; 若失败,返回EOF
*/

关闭一个文件流,使用fclose就可以把缓冲区内最后剩余的数据输出到磁盘文件中,并释放文件指针和有关 的缓冲区


demo

#include <stdio.h>
#include <string.h>

 int  main()
{
   FILE *fp;
   char *str = "liujiaxin";
   char readBuf[128]={0};

   fp= fopen("./liujiaxin.txt", "w+");

   fwrite(str,sizeof(char),strlen(str),fp);

   fseek(fp,0,SEEK_SET);
   fread(readBuf,sizeof(char),strlen(str),fp);
   printf("readdata:%s\n",readBuf);

   return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

LJX

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值