栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

Linux文件编程

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Linux文件编程

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

函数原型

SYNOPSIS
       #include  //这里提供类型pid_t和size_t的定义
       #include 
       #include 

       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 
#include 
#include 
#include 

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

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

2.write—文件写入操作

函数原型

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

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


文件关闭操作 

SYNOPSIS
       #include 
       int close(int fd);

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

demo

#include 
#include 
#include 
#include 
#include 
#include 

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

3.read—文件读取操作

函数原型

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

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

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

demo

#include 
#include 
#include 
#include 
#include 
#include 

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

     //打开文件
     if(fd == -1){
        printf("open file failn");
        fd = open("./file",O_RDWR|O_CREAT,0600);
        if(fd > 0){
                printf("creat file successn");
        }
     }
     printf("open file successn");
     //写入内容
     int n_write = write(fd,buf,strlen(buf));
     if(n_write != -1){
        printf("write successn");
     }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 %sn",n_read,BufRead);

     close(fd);
     return 0;
}
4.lseek—文件光标移动操作

函数原型

SYNOPSIS
       #include 
       #include 

       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 
#include 
#include 
#include 
#include 
#include 

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

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

      //使光标偏移到文件开头
      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 %sn",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)

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

注: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)

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)

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

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


fclose函数原型

int fclose(FILE*stream)

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


demo

#include 
#include 

 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:%sn",readBuf);

   return 0;
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/940551.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号