在学习Linux系统时,除了学习Linux常用命令后,还需要学习Linux API编程。Linux C函数参考手册带书签非常清晰.pdf

LinuxC和WindowsC

image.png

文件操作

image.png
image.png

文件流

image.png

fopen

image.png

非描述符读写文件

  1. #include <stdio.h>
  2. #include <errno.h>
  3. #include <string.h>
  4. void testCreateFile(){
  5. // 1. create file
  6. FILE *fp=fopen( "hello.txt","w+");
  7. if(fp==NULL) {
  8. printf("fopen err: %s\n",strerror(errno));
  9. return;
  10. }
  11. // 2. write file
  12. char szBuf[100] = {"hello 15pb"};
  13. size_t size = fwrite(szBuf, strlen(szBuf), 1, fp);
  14. if(size == 0) {
  15. printf(" fwrite err");
  16. fclose(fp);
  17. return;
  18. }
  19. //移动读写位置到文件尾部,
  20. fseek(fp,0,SEEK_END);
  21. //获取文件大小
  22. int file_size = ftell(fp);
  23. //获取完大小再把文件指针移到最开头,不然等下读不到文件
  24. fseek(fp,0,SEEK_SET);
  25. //read file
  26. char buf[1024]={};
  27. int ret = fread(buf,1024,1,fp);//如果file_size这个缓冲区太大了,虽然读进去了,但是返回值为0
  28. //如果读取大小超过实际大小,读取成功,但是返回值是0
  29. //如果失败,errno的值是非0
  30. if(ret==0 && errno!=0){
  31. //转换错误消息
  32. printf("fread err: %s\n",strerror(errno));
  33. }
  34. printf("file context: %s\n",buf);
  35. // 3. close file
  36. fclose(fp) ;
  37. }
  38. int main(){
  39. testCreateFile();
  40. }

描述符读写文件

  1. #include <stdio.h>
  2. #include <errno.h>
  3. #include <string.h>
  4. #include <unistd.h>
  5. #include <fcntl.h>
  6. //测试写方式
  7. // 0表示标准输入
  8. // 1表示标准输出
  9. // 2表示标准错误输出
  10. void writefile()
  11. {
  12. // 1.打开文件
  13. //参数1文件名
  14. //参数2打开方式
  15. //参数3文件权限
  16. //返回文件描述符
  17. int fd = open("./test.txt", O_CREAT | O_WRONLY, S_IRWXU);
  18. if (fd == 0)
  19. {
  20. //转换错误信息
  21. printf("open error : %s\n", strerror(errno));
  22. return;
  23. }
  24. //写入文件
  25. char buff[] = "hello 15pb\n";
  26. //参数1,文件描述符
  27. //参数2缓冲区
  28. //参数3缓冲区大小
  29. write(fd, buff, strlen(buff));
  30. //关闭文件
  31. close(fd);
  32. }
  33. void readfile()
  34. {
  35. int fd = open("./test.txt", O_RDONLY);
  36. if (fd == 0)
  37. {
  38. //转换错误信息
  39. printf("open error : %s\n", strerror(errno));
  40. return;
  41. }
  42. //读取文件内容
  43. char buff[1024] = {};
  44. int ret = read(fd,buff,1024);
  45. //如果读取大小超过实际大小,读取成功,但是返回值是0
  46. //如果失败,errno的值是非0
  47. if (ret == 0 && errno != 0)
  48. {
  49. //转换错误消息
  50. printf("fread err: %s\n", strerror(errno));
  51. }
  52. //显示内容
  53. printf("file context: %s\n",buff);
  54. //关闭文件
  55. close(fd);
  56. }
  57. int main()
  58. {
  59. //writefile();
  60. readfile();
  61. return 0;
  62. }

通过文件名和文件描述符来获取文件信息

  1. #include <stdio.h>
  2. #include <errno.h>
  3. #include <string.h>
  4. #include <unistd.h>
  5. #include <fcntl.h>
  6. #include <sys/stat.h>
  7. //获取文件信息方式1 .通过文件名
  8. void get_file_stat(){
  9. struct stat info={};
  10. //通过文件名获取文件信息
  11. //返回0表示成功,非零表示失败
  12. if(0!=stat("./test.txt",&info)){//if括号里边的是会执行的
  13. printf("get stat error: %s" ,strerror(errno));
  14. return;
  15. }
  16. //输出文件信息
  17. printf("file size %ld \n" ,info.st_size);
  18. return ;
  19. }
  20. //同过描述符获取
  21. void get_file_stat2(){
  22. int fd = open("./test.txt", O_RDONLY);
  23. if (fd == 0){
  24. //转换错误信息
  25. printf("open error : 8s\n", strerror(errno));
  26. return 0;
  27. }
  28. struct stat info={};
  29. //获取文件信息
  30. fstat(fd,&info);
  31. printf("file size = %ld",info.st_size);
  32. close(fd);
  33. }
  34. int main()
  35. {
  36. get_file_stat();
  37. return 0;
  38. }

linuxAPI

image.png
image.png

  1. #include <dirent.h>
  2. #include <stdio.h>
  3. #include <errno.h>
  4. #include <string.h>
  5. //遍历目录
  6. void enumdir(char *path)
  7. {
  8. //目录流结构体指针
  9. struct DIR *pdir;//打开目录
  10. pdir = opendir(path);
  11. if (pdir == NULL)
  12. {
  13. //转换错误消息
  14. printf("fread err: %s\n", strerror(errno));
  15. }
  16. struct dirent *info;
  17. int number=0;
  18. while (info =readdir(pdir))
  19. {
  20. //输出目录下文件信息
  21. printf("file type = %d\n", info->d_type);
  22. printf("file name = %s\n", info->d_name);
  23. number++;
  24. }
  25. printf("一共有 %d 个文件",number);
  26. //关闭文件
  27. closedir(pdir);
  28. }
  29. int main()
  30. {
  31. enumdir("/root");
  32. return 0;
  33. }