Chinaunix首页 | 论坛 | 博客
  • 博客访问: 300799
  • 博文数量: 134
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 118
  • 用 户 组: 普通用户
  • 注册时间: 2013-08-01 14:02
文章分类

全部博文(134)

文章存档

2015年(2)

2014年(4)

2013年(128)

分类: C/C++

2013-08-01 14:16:28

原文地址:文件操作_练习_0305 作者:丫叩酱


点击(此处)折叠或打开

  1. #Makefile

  2. #如果每个.c文件中都含有main函数,使用Makefile,可直接生成与之对应的可执行文件,不需要生成中间过程文件

  3. src = $(wildcard *.c)
  4. targets = $(patsubst %.c, %, $(src))

  5. CC = gcc
  6. CFLAGS = -Wall -g

  7. all:$(targets)
  8. $(targets):%:%.c
  9.     $(CC) $(CFLAGS) $< -o $@
  10. clean:
  11.     rm $(targets)
  12. .PHONY:clean

点击(此处)折叠或打开

  1. //fgets_fputs_cp.c

  2. #include <stdio.h>

  3. void cp(int argc, char *argv[])
  4. {
  5.     FILE *srcp, *destp;
  6.     char buff[1024];

  7.     if(argc != 3)
  8.         printf("Usage: cp src_file dest_file\n");
  9.     
  10.     srcp = fopen(argv[1], "r");
  11.     destp = fopen(argv[2], "w");

  12.     if(srcp == NULL || destp == NULL)
  13.     {
  14.         printf("open file failure!\n");
  15.     }

  16.     while((fgets(buff, sizeof(buff), srcp)) != NULL)
  17.         fputs(buff, destp);

  18.     fclose(srcp);
  19.     fclose(destp);
  20. }

  21. int main(int argc, char *argv[])
  22. {
  23.     cp(argc, argv);
  24.     return 0;
  25. }

点击(此处)折叠或打开

  1. //cat.c

  2. //实现cat指令功能,注意不要用>,系统自动捕捉,导致功能不能实现

  3. #include <stdio.h>

  4. void cat(int argc, char **argv)
  5. {
  6.    FILE *fp;
  7.    char buff[1024];

  8.    printf("cat\n");

  9.    if(argc == 2)
  10.    {
  11.         printf("argc = 2\n");
  12.         fp = fopen(argv[argc - 1], "r");
  13.         if(fp == NULL)
  14.             printf("open file failure!\n");
  15.         while((fgets(buff, sizeof(buff), fp)) != NULL)
  16.             fputs(buff, stdout);
  17.         fclose(fp);
  18.    }
  19.    if(argc == 3)
  20.    {
  21.         printf("argc = 3\n");
  22.         int ch;

  23.         fp = fopen(argv[argc - 1], "a");
  24.         if(fp == NULL)
  25.             printf("open file failure!\n");

  26.         while((ch = fgetc(stdin)) != EOF)
  27.             fputc(ch, fp);
  28.         fclose(fp);
  29.    }
  30.    
  31. }


  32. int main(int argc, char *argv[])
  33. {
  34.     cat(argc, argv);

  35.     return 0;
  36. }

点击(此处)折叠或打开

  1. //error.c

  2. //错误输出的简单实现

  3. #include <stdio.h>
  4. #include <errno.h>

  5. void myerror(void)
  6. {
  7.     FILE *fp;
  8.     fp = fopen("1.c", "r");
  9.     perror("no such file:");

  10. }

  11. int main(int argc, const char *argv[])
  12. {
  13.     myerror();
  14.     return 0;
  15. }

点击(此处)折叠或打开

  1. //error_sys.c

  2. //系统错误输出的简单实现

  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <errno.h>
  6. #include <stdarg.h>
  7. #include <string.h>

  8. #define MAXLINE     80

  9. void err_sys(const char *fmt,...)
  10. {
  11.         int err = errno;
  12.         char buf[MAXLINE+1];
  13.         va_list ap;
  14.         va_start(ap, fmt);

  15.         vsnprintf(buf, MAXLINE, fmt, ap);
  16.         snprintf(buf+strlen(buf), MAXLINE-strlen(buf), " : %s", strerror(err));
  17.         strcat(buf, "\n");
  18.         fputs(buf, stderr);

  19.         va_end(ap);
  20.         exit(1);
  21. }

  22. int main(int argc, char *argv[])
  23. {
  24.         FILE *fp;
  25.         if(argc != 2)
  26.         {
  27.                 fputs("Usage: ./a.out pathname\n", stderr);
  28.                 exit(1);
  29.         }
  30.         fp = fopen(argv[1], "r");
  31.         if(fp == NULL)
  32.         {
  33.                 err_sys("%s():%d - Open file %s", __func__, __LINE__, argv[1]);
  34.         }
  35.         printf("Open %s OK\n", argv[1]);
  36.         fclose(fp);
  37.         return 0;
  38.                 
  39. }

点击(此处)折叠或打开

  1. //log.c

  2. //自定义的日志文件的简单实现

  3. #include <stdio.h>
  4. #include <time.h>

  5. void mylog(char *str)
  6. {
  7.    time_t t;
  8.    struct tm *tm_str;
  9.    FILE *fp;

  10.    t = time(NULL);
  11.    tm_str = localtime(&t);

  12.    fp = fopen("log.txt","a");
  13.    if(fp == NULL)
  14.         printf("open file failure\n");

  15.    fprintf(fp, "%d-%d-%d %d:%d%d :%s\n", (tm_str->tm_year)+1900,tm_str->tm_mon+1,tm_str->tm_mday,tm_str->tm_hour,tm_str->tm_min,tm_str->tm_sec, str);

  16.    printf("%d-%d-%d %d:%d:%d :%s\n", tm_str->tm_year+1900,tm_str->tm_mon+1,tm_str->tm_mday,tm_str->tm_hour,tm_str->tm_min,tm_str->tm_sec, str);
  17. }

  18. int main(int argc, char *argv[])
  19. {
  20.     mylog("add a line");
  21.     mylog("delete a line");
  22.     return 0;
  23. }

点击(此处)折叠或打开

  1. //student_manager.c

  2. //简单学生管理系统的实现:
  3. //增、删、改、查

  4. #include <stdio.h>
  5. #include <string.h>
  6. int number = 3;
  7. typedef struct student
  8. {
  9.     int id;
  10.     char name[16];
  11.     int math;
  12.     int english;
  13.     float avg;

  14.     struct student *next;
  15. }stud_t;


  16. void init(stud_t *s, int num)
  17. {
  18.     int i;
  19.     for (i = 0; i < num; i++)
  20.     {
  21.         printf("id name math english\n");
  22.         scanf("%d %s %d %d",&s[i].id, s[i].name, &s[i].math, &s[i].english);
  23.     }
  24. }

  25. void average(stud_t *s, int num)
  26. {
  27.     int i;
  28.     for (i = 0; i < num; i++)
  29.     {
  30.         s[i].avg = ((float)(s[i].math + s[i].english))/2;
  31.     }
  32. }

  33. void show(stud_t *s, int num)
  34. {
  35.     int i;
  36.     printf("id name math english\n");
  37.     for (i = 0; i < num; i++)
  38.     {
  39.         printf("%d\t%s\t%d\t%d\t%f\n", s[i].id, s[i].name, s[i].math, s[i].english, s[i].avg);
  40.     }
  41. }



  42. void range(stud_t *s, int num)
  43. {
  44.     int i, j;
  45.     for (i = 0; i < num - 1; i++)
  46.     {
  47.         for (j = i + 1; j < num; j++)
  48.         {
  49.             stud_t tmp;
  50.             if((s[i].math + s[i].english) < (s[j].math + s[j].english))
  51.             {
  52.                 tmp = s[i];
  53.                 s[i] = s[j];
  54.                 s[j] = tmp;
  55.             }
  56.         }
  57.     }
  58. }


  59. void delete(stud_t *s, int num)
  60. {
  61.     int i = 0, j, del_num;
  62.     
  63.     printf("del_num = ");
  64.     scanf("%d", &del_num);

  65.     while(del_num != s[i].id && i < num)
  66.         i++;
  67.     if(i < num)
  68.     {
  69.         number--;
  70.         for (j = i; j < num - 1; j++)
  71.         {
  72.             s[j] = s[j + 1];
  73.         }
  74.     }
  75.     else
  76.         printf("delete_num not exist\n");

  77. }

  78. void guide(void)
  79. {
  80.     printf("0:guide\n");
  81.     printf("1:chioce\n");
  82.     printf("2:init\n");
  83.     printf("3:show\n");
  84.     printf("4:delete\n");
  85.     printf("5:range\n");
  86.     printf("6:exit\n");
  87. }

  88. void choice(stud_t *s, int num)
  89. {
  90.     int n;
  91.     guide();
  92.     while(1)
  93.     {
  94.         printf("choose(0~6):");
  95.         scanf("%d", &n);

  96.         if(n == 6)
  97.             break;
  98.         switch(n)
  99.         {
  100.             case 0:guide();break;
  101.             case 1:choice(s, num);break;
  102.             case 2:
  103.             {
  104.                 init(s, num);
  105.                 average(s, num);
  106.                 break;
  107.             }
  108.             case 3:show(s, num);break;
  109.             case 4:
  110.             {
  111.                 delete(s, num);
  112.                 num--;
  113.                 break;
  114.             }
  115.             case 5:range(s, num);break;
  116.             default:;
  117.         }
  118.     }
  119. }


  120. int main(int argc, const char *argv[])
  121. {
  122.     stud_t s[number];
  123.     choice(s, number);
  124.     return 0;
  125. }

阅读(575) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~