当前位置:   article > 正文

2024.7.26 作业

2024.7.26 作业

1、使用fgets统计给定文件的行号

代码:

/*******************************************/

文件名:io1.c

/*******************************************/

  1. #include <myhead.h>
  2. int main(int argc, char const *argv[])
  3. {
  4. //判断是否导入了一个要引用的文件
  5. if (argc != 2)
  6. {
  7. printf("input file error\n");
  8. printf("usage:./a.out filename\n");
  9. return -1;
  10. }
  11. //定义文件指针
  12. FILE *fp = NULL;
  13. if ((fp = fopen(argv[1], "r")) == NULL)
  14. {
  15. perror("fopen error");
  16. return -1;
  17. }
  18. //定义数组接收数据
  19. char buf[64] = "";
  20. //定义整型表示行数
  21. int num = 0;
  22. int num_keep[100] = {0};
  23. while (1)
  24. {
  25. bzero(buf, sizeof(buf));
  26. if (fgets(buf, sizeof(buf), fp) == NULL)
  27. {
  28. break;
  29. }
  30. //定义整型获取每行真实长度
  31. int len = strlen(buf);
  32. //判断是否有'\n'分行
  33. if (buf[len - 1] == '\n')
  34. {
  35. num++;
  36. }
  37. num_keep[num - 1] = num;
  38. }
  39. printf("该文件共有%d行\n", num);
  40. printf("该文件的行号分别为:\n");
  41. for (int i = 0; num_keep[i] != 0; i++)
  42. {
  43. printf("%d\t", num_keep[i]);
  44. }
  45. printf("\n");
  46. //关闭文件指针
  47. fclose(fp);
  48. return 0;
  49. }

结果:

2、使用fgets、fputs完成两个文件的拷贝

代码:

/*******************************************/

文件名:io2.c

/*******************************************/

  1. #include <myhead.h>
  2. int main(int argc, char const *argv[])
  3. {
  4. if (argc != 3)
  5. {
  6. printf("input file error\n");
  7. printf("usage:./a.out srcfile destfile\n");
  8. return -1;
  9. }
  10. FILE *sfp = fopen(argv[1], "r+");
  11. if (sfp == NULL)
  12. {
  13. printf("open srcfile error\n");
  14. return -1;
  15. }
  16. FILE *dfp = fopen(argv[2], "w");
  17. if (dfp == NULL)
  18. {
  19. printf("open destfile error\n");
  20. fclose(sfp);
  21. return -1;
  22. }
  23. // 写入数据到sfp
  24. fputs("Hello World!\n", sfp);
  25. fputs("Welcome to China\n", sfp);
  26. fputs("We are friends!\n", sfp);
  27. fputs("HaHaHaHa!\n", sfp);
  28. fputs("See you!\n", sfp);
  29. // 移动文件指针到文件开始
  30. rewind(sfp);
  31. // 读取sfp并写入dfp
  32. char buf[40] = "";
  33. while (fgets(buf, sizeof(buf), sfp) != NULL)
  34. {
  35. fputs(buf, dfp);
  36. }
  37. // 关闭文件
  38. fclose(sfp);
  39. fclose(dfp);
  40. // 重新打开文件进行读取
  41. sfp = fopen(argv[1], "r");
  42. if (sfp == NULL)
  43. {
  44. printf("open srcfile error\n");
  45. return -1;
  46. }
  47. printf("sfp中的内容为:\n");
  48. char bufn[20];
  49. while (fgets(bufn, sizeof(bufn), sfp) != NULL)
  50. {
  51. printf("%s", bufn);
  52. }
  53. fclose(sfp);
  54. // 检查dfp是否需要重新打开进行读取
  55. dfp = fopen(argv[2], "r");
  56. if (dfp == NULL)
  57. {
  58. printf("open srcfile error\n");
  59. return -1;
  60. }
  61. printf("dfp中的内容为:\n");
  62. char bufm[20];
  63. while (fgets(bufm, sizeof(bufm), sfp) != NULL)
  64. {
  65. printf("%s", bufm);
  66. }
  67. return 0;
  68. }

结果:

思维导图:

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小舞很执着/article/detail/919278
推荐阅读
相关标签
  

闽ICP备14008679号