系统调用是实际上就是之最底层的一个调用,面向的是硬件,由Linux操作系统自提供的IO系统调用,针对文件操作来说,系统调用是最直接的方式,如:open,close,read、write,需包含头文件 unistd.h
系统调用通常用域底层文件访问,例如在驱动程序中对设备⽂件的直接访问。
文件的基本操作主要包括:打开、读取、写入、检测存在
access:检测存在
open()//打开文件
close()//关闭文件
read()//读取文件内容
write()//给文件写入内容
lseek()//移动文件指针
stat()//获取文件状态
示例:
#include <unistd.h>#include <fcntl.h>#include <string.h>#include <stdio.h>#include <error.h>int main(){int fd;extern int errno;//声明errno全局变量fd=open("11.txt",O_RDWR);if(fd!=-1){printf("文件打开成功!");}else{printf("errno=%d",errno);printf("%s",strerror(errno));}close(fd);return 0;}
例2 :将一段代码的前面部分的数字区域去掉
#include<stdio.h>#include<string.h>#include<stdbool.h>/*函数名: split函数功能:按空格切割字符串参数:src 接收待处理的字符串part1 切割的第一部分part2 切割的第二部分返回值:能切割成两部分返回true,切换不到两部分为false*/bool split(const char *src,char *part1,char *part2)//切割字符串{// void Update_set_value(int a, int b)char flag=0;//标记是否为空格int start=0;//记录切割的起始位char count=0;for(int i=0;src[i]!='\0';i++){if(src[i]!=' ' && flag==0)//当遇到第一个空格后的字母,记录标记{flag=1;start=i;}if((src[i]==' '||src[i]=='(')&&flag==1){flag=0;int index=0;count=count+1;if(count==1)//对第一部分进行切割{for(int y=start;y<i;y++,index++){part1[index]=src[y];}part1[index]='\0';}if(count==2)//对第二部分进行切割{ index=0;for(int y=start;y<i;y++,index++){part2[index]=src[y];}part2[index]='\0';return true;}}}return false;}//检测第一部分的类型bool is_type(char *part1){char *type_name[6]={"void","int","bool","double","char","float"};//类型名for(int i=0;i<6;i++){if(strcmp(type_name[i],part1)==0)return true;}return false;}int main(){FILE *fp;char buff[100]={0};char part1[50]={0};char part2[50]={0};bool flag=0;int count=0;fp=fopen("d:\\aa.c","r");//打开需要检索的文件if(fp==NULL)return 0;//对文件进行扫描while(1){fgets(buff,100,fp) ;if(feof(fp)!=0)break;flag=split(buff,part1,part2) ;if(flag){if(is_type(part1)){count++;printf("第%d个函数名:%s\n",count,part2);}}}return 0;}
例3:利用函数完成 ls -l 这个命令实现的内容
#include <stdio.h>#include <unistd.h>#include <sys/types.h>#include <sys/stat.h>#include <dirent.h>#include <fcntl.h>#include <string.h>#include <time.h>int main(int argc,char *argv[]){if(argc == 2 && strcmp(argv[1],"-l")==0)//判断在执行命令的时候是否有 -l 这个命令提示符{char path[100]={0};//定义一个数组准备接受DIR *dp=NULL;struct dirent *dt = NULL;struct stat st;getcwd(path,100);dp=opendir(path);if(dp==NULL){printf("目录无效!");return 1;}while((dt=readdir(dp))!=NULL){stat(dt->d_name,&st);time_t t=time(NULL);t=st.st_atime;struct tm *gt;gt =gmtime(&t);switch(dt->d_type){case 1:printf("p");break;//管道文件case 2:printf("c");break;//字符设备文件case 4:printf("d");break;//目录文件case 6:printf("b");break;//块设备文件case 8:printf("-");break;//一般文件case 10:printf("l");break;//符号链接文件case 12:printf("s");break;//套接字文件case 4:printf("d");break;}switch(st.st_mode & 0700){case 0700:printf("rwx");break;case 0400:printf("r--");break;case 0200:printf("-w-");break;case 0100:printf("--x");break;case 0600:printf("rw-");break;case 0500:printf("r-x");break;case 0300:printf("-wx");break;case 0000:printf("---");break;}printf(" ");printf("%3ld",st.st_nlink);printf(" ");printf("%2d",st.st_uid);printf(" ");printf("%2d",st.st_gid);printf(" ");printf("%5ld",st.st_size);printf(" ");printf("%d %d %2d:%2d",gt->tm_mon+1,gt->tm_mday,(gt->tm_hour+8)%24,gt->tm_min);printf(" ");printf("%s\n",dt->d_name);}closedir(dp);}else{printf("选项错误!");}return 0;}
