[Linux C编程]文件操作

本文详细介绍了Linux系统调用和C语言编程中关于文件操作的内容,包括系统调用如creat、open、read、write、lseek,以及C标准库函数fopen、fputc、fgetc、fputs、fgets等的使用。文章阐述了系统调用与API的关系,文件描述符的概念,并探讨了各种文件打开模式及其应用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

文件操作

1. Linux系统调用及用户编程接口(API)

    所谓系统调用是指操作系统提供给用户的一组“特殊”接口,用户程序可以通过这组“特殊”接口来获得操作系统内核提供的的服务

为什么用户程序不能直接访问系统内核提供的服务呢?

 

由于在Linux中,为了更好地保护内核空间,将程序的运行空间分为内核空间和用户空间(也就是常称的内核态和用户态),它们分别运行在不同的级别上,在逻辑上是相互隔离的。

 

系统调用并不是直接与程序员进行交互的,它仅仅是一个通过软中断机制向内核提交请求,以获取内核服务的接口。在实际使用中程序员调用的通常是用户编程接口—API

1.函数的作用 2.函数的参数 3.函数的返回值

 

Linux 文件:

“一切皆为文件”——在Linux中对目录和设备的操作都等同于对文件的操作。

普通文件、目录文件、链接文件、设备文件

 

文件及文件描述符——fd

文件描述符是一个非负的整数,它是一个索引值,并指向在内核中每个进程打开文件的记录表。

 

系统调用——创建creat

函数的作用:创建一个文件

函数的原型:int creat(const char *pathname,mode_t mode);

文 件 头:#include <sys/types.h>

          #include <sys/stat.h>

          #include <fcntl.h>

返 回 值:成功:新的文件描述符

          出错:-1

mode:创建模式

S_IRUSR:可读

S_IWUSR:可写

S_IXUSR:可执行
    S_IXRWU:可读、可写、可执行

#include <stdio.h>
#include <stdlib.h>
 
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
 
void  create_file(char *filename)
{
    if(creat(filename,0755)<0){
        printf("create file %s failure!\n",filename);
        exit(EXIT_FAILURE);
    }else{
        printf("create file %s success!\n",filename);
    }
}
 
int main(int argc,char *argv[])
{
    int i;
     
    if(argc<2)
    {
        perror("you haven't input the filename,please try again!\n");
        exit(EXIT_FAILURE);
    }
 
    for(i=1;i<argc;i++)
    {
        create_file(argv[i]);    
    }
 
    exit(EXIT_SUCCESS);
}


 

系统调用——打开open

函数的作用:打开一个文件;

函数的原型:int  open(const char *pahtname, int flags);

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

返 回 值:文件描述符---成功;出错:-1;

flags:

参数:

O_RDONLY:  只读

 O_WRONLY:  只写

O_RDWR:  可读可写

        O_CREAT:  如果原来这个文件不存在,那么有这个参数就可以创建这个文件;

        O_APPEND: 原来有内容,则会自动保留文件内容,自动向下读写;

        O_TRUNC:  文件存在,有内容,文件清空;

#include <stdio.h>
#include <stdlib.h>
 
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
 
int main(int argc ,char *argv[])
{
    int fd;
    
    if(argc<2)
    {
        puts("please input the open file pathname!\n");
        exit(1);
    }
    
    //如果flag参数里有O_CREAT表示,该文件如果不存在,系统则会创建该文件,该文件的权限由第三个参数决定,此处为0755
    //如果flah参数里没有O_CREAT参数,则第三个参数不起作用.此时,如果要打开的文件不存在,则会报错.
    //所以fd=open(argv[1],O_RDWR),仅仅只是打开指定文件
    if((fd=open(argv[1],O_CREAT|O_RDWR,0755))<0)
    {
        perror("open file failure!\n");
        exit(1);
    }else{
        printf("open file %d  success!\n",fd);
 
    }
    close(fd);
    exit(0);
    
}   


 

 系统调用——读read

 函数的作用: 从打开的文件中读取数据

 函数的原型:ssize_t  read(int fd, void *buf,  size_t count);

 头 文 件: #include  <unistd.h>

 返 回 值:正常是实际读到的字节数;

           如果是在文件结束或者是无数据,返回0;

           出错,-1;

 

 系统调用——写write

 函数的作用: 向打开的文件中写数据

 函数的原型: ssize_t   write(int fd, const void *buf, size_t count);

 头 文 件:  #include <unistd.h>  

 返 回 值:  成功会返回实际写入的字节数;

             出错:-1;

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
 
#define LENGTH 100
 
int main()
{
    int fd;
    int len;
 
    char str[LENGTH];
 
    fd = open("hello.txt",O_CREAT|O_RDWR,S_IRUSR|S_IWUSR);
 
    if(fd > 0)
    {
        write(fd,"Hello,Software weekly",strlen("Hello,Software weekly"));
//close(fd);
    }
    
    fd = lseek(fd,0,SEEK_SET);
    fd = open("hello.txt",O_RDWR);
    
    len = read(fd,str,LENGTH);
    str[len] = '\0';
 
    printf("read length is %d\n",len);
    printf("%s\n",str);
 
    close(fd);
 
    return 0;
}         


 

系统调用——定位lseek

函数的功能:进行文件定位

函数的原型: int lseek(int fd, offset_t  offset, int whence);

函数的参数:fd:

            offset: 指针的微调,在指定的指针向前移动为负, 向后为正;

            whence:  SEEK_SET:放在文件头

             SEEK_CUR:当前的位置;

             SEEK_END:  文件尾;

            

返 回 值:返回文件当前指针到文件开始的地方有多少字节;

   出错-1;

  

示例:拷贝函数:

#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
 
#define BUFFER_SIZE 1024
 
int main(int argc, char *argv[])
{
    int write_fd;  
    int read_fd;   
 
    int write_byte;    
    int read_byte;     
 
    char buffer[BUFFER_SIZE];
    char *ptr;
 
    if(argc != 3)
    {
        printf("Please try again,the number of para is less than 3!\n");
    }
 
    if((read_fd = open(argv[1],O_RDONLY)) == -1)
    {
        printf("read_fd open failure!\n");
    }
 
    if((write_fd = open(argv[2],O_WRONLY|O_CREAT,S_IRUSR|S_IWUSR)) == -1)
    {
        printf("write_fd open failure!\n");
    }
 
    while(read_byte = read(read_fd,buffer,BUFFER_SIZE))  
    {
        if((read_byte == -1)&&(errno != EINTR))  
        {
            break;
        }
        else if(read_byte > 0)
        {
            ptr = buffer;   
            
            while(write_byte = write(write_fd,ptr,read_byte))  
            {
                if((write_byte == -1)&&(errno != EINTR))
                {
                    break;
                }
                else if(write_byte == read_byte)  
                {
                    break;
                }
                else if(write_byte > 0)  
                {
                    ptr += write_byte;
                    read_byte -= write_byte;
                }
            }
            if(write_byte == -1)
            {
                break;
            }
        }
    }
 
    close(read_fd);
    close(write_fd);
    
    return 0;
}


标准库函数

·不带缓存的I/O对是文件描述符操作,带缓存的I/O是针对流的。

·标准I/O库就是带缓存的I/O,它由ANSI C标准说明。当然,标准I/O最终都会调用上

  面的I/O例程。

·标准I/O库代替用户处理很多细节,比如缓存分配、以优化长度执行I/O等。

它提供了三种类型的缓存:
1) 全缓存。当填满标准I/O缓存后才执行I/O操作。磁盘上的文件通常是全缓存的。
2) 行缓存。当输入输出遇到新行符或缓存满时,才由标准I/O库执行实际I/O操作。stdin、

   stdout通常是行缓存的。
3) 无缓存。相当于read、write了。stderr通常是无缓存的,因为它必须尽快输出。

 

库函数——打开fopen

函数的作用: 打开文件

函数的原型: FILE *fopen(const char *pth, const char *mode)

 mode:

   r:读,文件必须存在;

   r+:打开可读写,文件必须存在;

   w:打开只写文件,文件不存在就会创建文件; 文件清0;

   w+:打开可读写的文件,

   a:附加的形式打开只写文件,不存在就创建,存在就写到原来的文件尾。

   a+:以附加的形式打开可读写的文件,不存在就创建,存在就写到原来的文件尾。

   b:二进制文件

文 件 头: #include <stdio.h>      #include  "  "

返 回 值:  成功是指向=文件流的指针;

            出错返回NULL;     

 

#include <stdio.h>
int main()
{
FILE * fp;
fp = fopen(“hello.c”,“r+”);
if(fp != NULL)
{
    printf(“fopen is ok!\n”);
}
fclose(fp);
    return 0;
}
 


 库函数——字符写fputc

 函数的作用:  将一个指定的字符写入到文件流中;

 函数的原型:  int fputc(int c, FILE *stream);

 返 回 值:   返回写入成功的字符,c;  EOF则表示失败。

 

 库函数——字符读fgetc

 函数的作用:从文件流中读取一个字符

 函数原型: int fgetc(FILE *stream)

 返 回 值:返回值正常的是读取的字符;EOF表示到了文件尾;

 

库函数——字符串写fputs

函数的作用:将一个字符串写入到文件内

函数的原型: int fputs(const char *s, FILE *stream)

返 回 值:成功返回写成字符数; EOF表示出错

#include <stdio.h>
#define LENGTH 100
main()
{
FILE *fd;
char str[LENGTH];
 
fd = fopen("hello.txt", "w+"); /* 创建并打开文件 */
if (fd)
{
fputs("Hello, Software Weekly", fd); /* 写入Hello, software weekly字符串 */
fclose(fd);
}
 
fd = fopen("hello.txt", "r");
fgets(str, LENGTH, fd); /* 读取文件内容 */
printf("%s\n", str);
fclose(fd);
}


 

 库函数——字符串读fgets

 函数的作用:从文件中读取一个字符串;

 函数的原型: char *fgets(char *s, int size,  FILE *steam)

 函数的参数:从stream中读size-1个字符到s中

 返 回 值: 成功返回s, 出错NULL。

 

库函数——数据块读 fread

函数的作用:从文件流中读取数据块

函数原型:  size_t  fread(void *ptr,  size_t size, size_t  nmemb, FILE * stream);

返 回 值: 返回实际读到数据块的数目

           比nmember小的话,可能是到了文件尾,或者错误发生。

           feof :到文件尾;

           ferror:判断错误的

库函数——数据块写fwrite

函数的作用: 将数据块写到文件流中:

函数原型:  size_t  fwrite(const void * ptr,  size_t size,  size_t nmemb,  FILE *stream);

返回值: 实际写入的nmemb数目;

 

库函数——fseek

函数的作用:移动文件流的读写位置

函数的原型: int fseek(FILE * stream, long offset, int whence)

返 回 值:成功返回0, 出错-1;

****使用ftell来获取当前的位置;

 

库函数——ftell

函数的作用:读取文件流的读写位置;

函数的原型:long ftell(FILE * stream)

返回值: 成功返回当前的读写位置;

         出错-1;

         

库函数——feof

函数的作用: 检测文件流是否到了文件尾

函数的原型:int feof(FILE *steam)

返回值:  非零代表到了文件尾,其他是0;

 

库函数——格式化读fprintf

函数的作用:格式化数据到文件

函数的原型: int fprintf(FILE *stream, const char *format, ....);

返回值:成功返回实际输入的字符数,失败-1;

 

库函数——格式化写fscanf

函数的作用: 格式化字符串输入

函数的原型: int fscanf(FILE *strem, const char *fromat,....)

返回值:成功返回参数数目,出错-1

 

用库函数实现file_cp.c

#include <string.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
 
#define BUFFER_SIZE 1024
 
int main(int argc,char **argv)
{
FILE *from_fd;
FILE *to_fd;
long file_len=0;
char buffer[BUFFER_SIZE];
char *ptr;
/*判断入参*/
if(argc!=3)
{
printf("Usage:%s fromfile tofile\n",argv[0]);
exit(1);
}
 
/* 打开源文件 */
if((from_fd=fopen(argv[1],"rb"))==NULL)
{
printf("Open %s Error\n",argv[1]);
exit(1);
}
 
/* 创建目的文件 */
if((to_fd=fopen(argv[2],"wb"))==NULL)
{
printf("Open %s Error\n",argv[2]);
exit(1);
}
 
/*测得文件大小*/
fseek(from_fd,0L,SEEK_END);
file_len=ftell(from_fd);
fseek(from_fd,0L,SEEK_SET);
printf("from file size is=%d\n",file_len);
 
/*进行文件拷贝*/
while(!feof(from_fd))
{
fread(buffer,BUFFER_SIZE,1,from_fd);
if(BUFFER_SIZE>=file_len)
{
fwrite(buffer,file_len,1,to_fd);
}
else
{
fwrite(buffer,BUFFER_SIZE,1,to_fd);
file_len=file_len-BUFFER_SIZE;
}
memset(buffer, 0, BUFFER_SIZE);
//bzero(buffer,BUFFER_SIZE); //清零
}
fclose(from_fd);
fclose(to_fd);
exit(0);
}
 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值