学习Linux环境编程 读取文件和目录是很重要的环节,因为Linux环境下 "一切皆文件"
首先得明白文件描述符的概念,每一个进程都会默认打开标准输入,标准输出,标准出错 下面这个案列很好的解释文件描述符的概念
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
int main()
{
char* path = "shao.txt";
close(STDOUT_FILENO);
int fd1 = open("/dev/pts/1",O_WRONLY);
int fd = open(path,O_RDONLY);
if(fd == -1)
{
printf("%s\n",strerror(errno));
}else
{
while(1)
{
printf("fd1 = %d;fd = %d\n",fd1,fd);
sleep(1);
}
close(fd);
}
printf("shaozhongqi\n");
return EXIT_SUCCESS;
}
~
~
~
~
~
~
~
Linux系统文件描述符是一个有序的列表 0 1 2 3...进程中把标准输出1关闭掉 打开另一个文件 这个文件就变成标准输出 printf()本质就是在标准输出打印,所以会在另一个控制台打印数据。
一: Linux文件操作有两种系统调用 和 C/C++库函数 首先讲一下系统调用 有系统提供的操作接口
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
int main(int arg,char*args[])
{
if(arg < 2)
{
return 0;
}else
{
char* filepath = args[1];
int fd = open(filepath,O_RDONLY);
if(fd == -1)
{
printf("%s\n",strerror(errno));
return 0;
}else
{
//read file to buf
char buf[100];
memset(buf,0,sizeof(buf));
while(read(fd,buf,sizeof(buf)-1) > 0)
{
printf("%s\n",buf);
memset(buf,0,sizeof(buf));
}
close(fd);
}
}
return EXIT_SUCCESS;
}
写文件的系统调用:
二:Linux环境下用C库函数进行文件操作:
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
int main(int arg,char*args[])
{
if(arg < 2)
{
return 0;
}else
{
char* filepath = args[1];
FILE* fp = fopen(filepath,"r+");
if(fp == NULL)
{
printf("%s\n",strerror(errno));
return 0;
}else
{
char buf[10];
memset(buf,0,sizeof(buf));
size_t rc = 0;
while(1)
{
size_t tmp = 0;
tmp = fread(buf,1,10,fp);
rc = rc + tmp;
if(tmp == 0)
{
break;
}
}
printf("rc = %d\n",rc);
}
}
return EXIT_SUCCESS;
}
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
int main(int arg,char* args[])
{
if(arg < 2)
{
return 0;
}else
{
char* filepath = args[1];
FILE* fp = fopen(filepath,"a");
if(fp == NULL)
{
printf("%s\n",strerror(errno));
return 0;
}else
{
char buf[100];
memset(buf,0,sizeof(buf));
strcpy(buf,"shaozhongqi come from NanJing\n");
fwrite(buf,strlen(buf),1,fp);
fclose(fp);
}
}
return EXIT_SUCCESS;
}
~
三:用C函数库进行二进制文件的读写操作:
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
typedef struct tag_Teacher
{
char name[10];
int age;
char tel[10];
}Teacher;
int main(int arg,char*args[])
{
if(arg < 2)
{
return 0;
}else
{
char* filepath = args[1];
FILE* fp = fopen(filepath,"r+");
if(fp == NULL)
{
printf("%s\n",strerror(errno));
return 0;
}else
{
Teacher mteacher;
while(fread(&mteacher,sizeof(Teacher),1,fp)>0)
{
printf("name:%s\n",mteacher.name);
printf("age:%d\n",mteacher.age);
printf("tel:%s\n",mteacher.tel);
}
fclose(fp);
}
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
typedef struct tag_Teacher
{
char name[10];
int age;
char tel[10];
}Teacher;
int main(int arg,char* args[])
{
if(arg < 2)
{
return 0;
}else
{
char* filepath = args[1];
FILE* fp = fopen(filepath,"w");
if(fp == NULL)
{
printf("%s\n",strerror(errno));
return 0;
}else
{
Teacher teachers[10];
memset(teachers,0,sizeof(teachers));
strcpy(teachers[0].name,"shao111");
teachers[0].age = 11;
strcpy(teachers[0].tel,"11111111");
strcpy(teachers[1].name,"shao222");
teachers[1].age = 22;
strcpy(teachers[1].tel,"2222222");
fwrite(teachers,sizeof( Teacher),2,fp);
fclose(fp);
}
四:利用C函数库在linux环境下打印log
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#include <time.h>
void writelog(const char* log)
{
time_t tDate;
struct tm* eventTime;
time(&tDate); //get system time
eventTime = localtime(&tDate);
int year = eventTime->tm_year + 1900;
int month = eventTime->tm_mon + 1;
int day = eventTime->tm_mday;
int hour = eventTime->tm_hour;
int min = eventTime->tm_min;
int sec = eventTime->tm_sec;
char sDate[16];
sprintf(sDate,"%04d-%02d-%2d",year,month,day);
char sTime[16];
sprintf(sTime,"%02d-%2d-%2d",hour,min,sec);
char str[1024];
sprintf(str,"%s-%s-%s\n",sDate,sTime,log);
FILE* fp = fopen("mylog.txt","a+");
if(fp == NULL)
{
printf("write log erro:%s\n",strerror(errno));
}else
{
fputs(str,fp);
fclose(fp);
}
return;
}
int main(int arg,char* args[])
{
writelog("begin write log");
printf("hello world\n");
writelog("end write log");
五:系统调用进行目录的操作
int main(int arg,char*args[])
{
if(arg < 2)
{
return 0;
}else
{
DIR* dp;
struct dirent* dirp;
dp = opendir(args[1]);
if(dp == NULL)
{
printf("open error ",strerror(errno));
return 0;
}
while((dirp = readdir(dp)) != NULL )
{
printf("%s\n",dirp->d_name);
}
closedir(dp);
}
return EXIT_SUCCESS;
}
~
~
Ok 文件操作到此结束 里面很多细节要记得清晰啊~~~