linux c目录与普通文件

本文介绍Linux环境下使用C语言进行文件和目录操作的基本方法,包括创建、删除文件与目录,以及如何打开、关闭、读写文件。

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

文章内容参考21天学通linux c编程


目录函数

mkdir     rmdir

文件函数

creat   remove  

open close  write  read  leek



文章内容参考21天学通linux c编程


目录函数

mkdir     rmdir

新建目录

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h> 
#include <errno.h>
main()
{ 
   extern int errno;
   char *path="/root/tmp11";
   
   if(mkdir(path,0766)==0)
   {
   	  printf("created the directory %s.\n",path);
   }
   else
   {   	  
   	  printf("cant't creat the directory %s.\n",path);
   	  printf("errno:%d\n",errno);
   	  printf("ERR  :%s\n",strerror(errno));	
   }
} 

 
删除目录
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h> 
#include <errno.h>
main()
{ 
   extern int errno;
   char *path="/root/tmp11";
   
   if(rmdir(path)==0)
   {
   	  printf("deleted the directory %s.\n",path);
   }
   else
   {   	  
   	  printf("cant't delete the directory %s.\n",path);
   	  printf("errno : %d\n",errno);
   	  printf("ERR  : %s\n",strerror(errno));	
   }
} 

 



文件函数

creat   remove  

open close  write  read  leek

---新建文件

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
main()
{ 
   extern int errno;
   char *path="/root/tmp.txt";
   
   if(creat(path,0766)==-1)
   {
  	  printf("cant't create the file %s.\n",path);
   	  printf("errno:%d\n",errno);
   	  printf("ERR  :%s\n",strerror(errno));
   }
   else
   {   	  
      printf("created file %s.\n",path);	
   }
} 
-----删除
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
main()
{ 
   extern int errno;
   char path[]="mytemp";
   
   if(remove(path)==0)
   {
      printf("Deleted file %s.\n",path);	
   }
   else
   {
      printf("cant't delete the file %s.\n",path);
   	  printf("errno: %d\n",errno);
   	  printf("ERR  : %s\n",strerror(errno));
   }
} 
打开文件

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
main()
{ 
   int fd,fd1;
   char path[]="/root/txt1.txt";
   extern int errno; 
   fd=open(path,O_WRONLY,0766);
   if(fd!=-1)
   {
      printf("opened file %s  .\n",path);	
   }
   else
   {
      printf("cant't open file %s.\n",path);
   	  printf("errno:%d\n",errno);
   	  printf("ERR  :%s\n",strerror(errno));
   }
   
   fd1=open(path,O_WRONLY|O_CREAT,0766);
   if(fd1!=-1)
   {
      printf("opened file %s  .\n",path);	
   }
   else
   {
      printf("cant't open file %s.\n",path);
   	  printf("errno:%d\n",errno);
   	  printf("ERR  :%s\n",strerror(errno));
   }
} 
关闭文件
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
main()

   int fd;
   char path[]="/root/txt1.txt";
   extern int errno; 
   
   fd=open(path,O_RDONLY ,0766);
   if(fd!=-1)
   {
      printf("opened file %s  .\n",path);
   }
   else
   {
      printf("cant't open file %s.\n",path);
     printf("errno:%d\n",errno);
     printf("ERR  :%s\n",strerror(errno));
   }
   
   if(close(fd)==0)
   {
     printf("closed.\n");
   }
   else
   {
      printf("close file %s error.\n",path);
     printf("errno:%d\n",errno);
     printf("ERR  :%s\n",strerror(errno));
   }  
   
    if(close(1156)==0)
   {
     printf("closed.\n");
   }
   else
   {
      printf("close file %s error.\n",path);
     printf("errno:%d\n",errno);
     printf("ERR  :%s\n",strerror(errno));
   }


写入

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
main()
{ 
   int fd;
   char path[]="/root/txt1.txt";
   char s[]="hello ,Linux.\nI've leart C program for two weeks.\n";
   extern int errno; 
   
   fd=open(path,O_WRONLY|O_CREAT);
   if(fd!=-1)
   {
      printf("opened file %s  .\n",path);	
   }
   else
   {
      printf("cant't open file %s.\n",path);
   	  printf("errno:%d\n",errno);
   	  printf("ERR  :%s\n",strerror(errno));
   }
   write(fd,s,sizeof(s));
   close(fd);  
   printf("Done");
} 
读文件

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
main()
{ 
   int fd;
   char path[]="/root/a.txt";
   int size;
   char s[100];
   extern int errno; 
   
   fd=open(path,O_RDONLY);
   if(fd!=-1)
   {
      printf("opened file %s  .\n",path);	
   }
   else
   {
      printf("cant't open file %s.\n",path);
   	  printf("errno:%d\n",errno);
   	  printf("ERR  :%s\n",strerror(errno));
   }
   
   size=read(fd,s,sizeof(s));
   close(fd);
   printf("%s\n",s);
   printf("%d\n",size);
   close(fd);  
} 


读取重定位

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
main()
{ 
   int fd;
   char path[]="/root/txt1.txt";
   int size;
   char s[100]="";
   extern int errno; 
   
   fd=open(path,O_RDONLY);
   if(fd!=-1)
   {
      printf("opened file %s  .\n",path);	
   }
   else
   {
      printf("cant't open file %s.\n",path);
   	  printf("errno:%d\n",errno);
   	  printf("ERR  :%s\n",strerror(errno));
   }
   
   size=read(fd,s,3);    
   printf("%d  :",size);
   printf("%s\n",s);
  
   size=read(fd,s,3); 
   printf("%d  :",size);
   printf("%s\n",s);
   
   lseek(fd,8,SEEK_SET);
   size=read(fd,s,3);
   printf("%d  :",size);
   printf("%s\n",s);

   lseek(fd,0,SEEK_SET);
   size=read(fd,s,3);
   printf("%d  :",size);
   printf("%s\n",s);
         
   close(fd);  
} 





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值