C语言中对文件操作的方式

本文介绍了C语言在Linux系统中对文件操作的方法,包括系统调用和库函数。详细讲述了创建文件、打开文件、读文件、写文件、关闭文件以及定位、访问判断等操作,并提供了示例代码。

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

C语言中对文件操作方式大致可以分为二大类,它们分别是:系统调用的方式访问文件和C语言库函数的方式访问文件 ,而这二种方式对文件的操作不外乎是

创建文件,打开文件,写文件,读文件 和关闭文件(以下所讲述的内容主要是针对Linux系统,而对于C语言库函数对文件的操作具有移植性各个操作系统上是可以通用的)


一,系统调用的方式对文件进行操作.

   1,创建文件

对于创建一个文件可以调用系统函数: int creat(const char *filename,mode_t mode)  返回文件的描述符;

filename: 是要创建的文件名

mode:是以什么样的模式去创建文件

常见的创建模式有:

S_IRUSE    可读

S_IWUSE   可写

S_IXUSE    可执行


除了表示创建模式外我们还可以使用:

1  ---------------可执行,

2----------------可写

4 ----------------可读

请看下例:


   1   #include <stdio.h>

  2    #include <stdlib.h>

   3   #include <sys/types.h>

  4   void create_file(char *filename){

  5     int result;
  6     if((result = creat(filename,0755)) < 0 ){
  7         printf("Create file meet wrong\n");
  8     }else {
  9        printf("get the return result value: %d \n",result);
 10         printf("create file success\n");
 11         printf("get the filename size %d ======\n",sizeof(filename));
 12     }
 13 }
 14 
 15 int main(int argc,char *argv[]){
 16     int i ;
 17     if(argc < 2){
 18         printf("please input the file name \n");
 19     }
 20 
 21     for(i = 1; i < argc; i++){
 22         printf("get the argv:  %d\n",sizeof(argv[i]));
 23         create_file(argv[i]);
 24     }

 25   exit(0);

 26 }


2,打开文件

open(const  char *pathname,flags);

open(const char *pathname,flags,mode_t mode)  说明:当打开一个文件时若存文件不存在则以mode方式创建此文件并以flags标志打开此文件;

常见的打开标志如下:

O_RDONLY 只读方式打开

O_WRONLY只写方式打开

O_RDWR读写方式打开

O_APPEND以追加方式打开

O_CREAT创建一个文件

O_NOBLOCK非阻塞方式打开


示例代码如下:

  1 #include <stdio.h>
  2 #include <sys/types.h>
  3 #include <fcntl.h>
  4 #include <sys/stat.h>
  5 
  6 int main(int argc, char *argv[]){
  7     int fd;
  8 
  9     if(argc < 2){
 10          puts("please input the filename path");
 11     }
 12 
 13     if((fd = open(argv[1],O_CREAT|O_RDWR,0755)) < 0){
 14             perror("open file failure!\n");
 15             exit(1);
 16       }else {
 17          printf("open file %d success \n",fd);
 18       }
 19 
 20       close(fd);
 21       exit(0);
 22 
 23 
 24 
 25 }


3,关闭文件

打开一个文件之后一定要将此文件关闭,从而释放出此文件的文件描述以供其它的文件使用

int close(int fd)

fd:代表的是文件描述符

上面的代码示例中已经用到


4,读文件

   int read(int fd , void *buffer,size_t nbytes) ;

此函数的含义为:从文件描述符为fd的文件中读入 nbytes个字节放入buffer所指向地址中,返回的是实际读入的字节数,如果返回的是0则表明未读入任何数据,如果返回是-1

,则表明读入时发生了错误.


5,写文件

  int write(int fd, void *buffer, size_t nbytes);

此函数的含义为:从buffer所指向的地址空间中读出nbytes个字节放入文件描述符为fd的文件中,返回的是实际读入的字节数,类似于read函数


6,定位

int lseek(int fd,offset_t offset ,int wherece);

功能:将文件描述符为fd文件的读写指针相对于wherece移动offset个字节;操作成功时,返回文件指针相对于文件头的位置.

其中的wherece可以使用以下的值:

SEEK_SET : 相对于文件开头

SEEK_CUR : 相对于文件读写指针当前的位置 

SEEK_END : 相对于文件的末尾位置 


7,访问判断

int access(const char *pathname,int mode);

含义:判断pathname所指向的文件是否具有mode所描述的权限,如果有则返回0,否则返回-1

mode的值可以取以下的值:

R_OK : 文件可读

W_OK : 文件可写

X_OK :  文件可执行

F_OK : 文件存在


如下示例:

  1 #include <unistd.h>
  2 #include <stdio.h>
  3 int  main(void){
  4     if(access("/etc/passwd",R_OK) == 0 ){
  5         printf("this file can read \n");
  6     }
  7 }


以下示例是从文件first_file中的内容复制到second_file中去.

  1 #include <stdio.h>
  2 #include <unistd.h>
  3 #include <stdlib.h>
  4 #include <sys/types.h>
  5 #include <sys/stat.h>
  6 #include <fcntl.h>
  7 
  8 
  9 #define BUFFER 1024
 10 
 11 
 12 int main(int argc, char *argv[]){
 13      char buffer_size[BUFFER];
 14 
 15      int first_fd;
 16      int second_fd;
 17 
 18      if(argc < 2){
 19          printf("please input the first_file content \n");
 20      }
 21 
 22 
 23 
 24      if((first_fd = open("first_file",O_CREAT|O_RDWR,0755)) < 0){
 25          printf("open first_file failure\n");
 26      }
 27 
 28      read(first_fd,buffer_size,1024);
 29      printf("get the buffer content:  %s",buffer_size);
 30 
 31      if((second_fd = open("second_file",O_CREAT|O_RDWR,0755)) < 0){
 32          printf("open second_file failure\n");
 33      }
 34 
 35      if(write(second_fd,buffer_size,1024) >0){
 36          printf("copy file get success\n");
 37      }else {
 38          printf("copy file get failure\n");
 39      }
 40 
 41      close(first_fd);
 42      close(second_fd);
 43      
 44     exit(0);
 45 } 













   

    



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值