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

全部博文(134)

文章存档

2015年(2)

2014年(4)

2013年(128)

分类: C/C++

2013-08-01 14:16:24


点击(此处)折叠或打开

  1. //fopen.c

  2. //文件操作3步曲:
  3. // fopen打开要操作的文件
  4. // 用文件操作库函数实现对文件的操作
  5. // fclose关闭文件

  6. //用fgets函数,获得文件的一行(知道遇到'\n')
  7. //fputs函数,输出文件的一行

  8. #include <stdio.h>

  9. int main(int argc, const char *argv[])
  10. {
  11.     FILE *fp;
  12.     char buff[128];

  13.     if((fp = fopen("1.c", "a")) == NULL)
  14.         printf("open failure\n");
  15.     
  16.     fgets(buff, sizeof(buff), stdin);

  17.     fputs(buff, fp);

  18.     
  19.     fclose(fp);
  20.     return 0;
  21. }

点击(此处)折叠或打开

  1. //fgetc_fputc_cp.c

  2. //用fgetc函数,实现copy函数功能

  3. #include <stdio.h>

  4. void copy(char *src, char *dest)
  5. {
  6.     FILE *fp1, *fp2;
  7.     char ch;

  8.     fp1 = fopen(src, "r");
  9.     fp2 = fopen(dest, "w");
  10.     if((fp1 == NULL) || (fp2 == NULL))
  11.         printf("%s/%s open failure\n", src, dest);

  12.     while((ch = fgetc(fp1)) != EOF)
  13.         fputc(ch, fp2);
  14.     fclose(fp1);
  15.     fclose(fp2);
  16. }

  17. int main(int argc, char *argv[])
  18. {
  19.     if(argc != 3)
  20.         printf("Usage: copy src_file dest_file\n");
  21.     else
  22.         copy(argv[1], argv[2]);

  23.     return 0;
  24. }

点击(此处)折叠或打开

  1. //fgets.c
  2. //练习使用fgets、gets、getchar、getc函数


  3. #include <stdio.h>

  4. int main(int argc, const char *argv[])
  5. {
  6.     char str[128] = {0};
  7.     char ch;
  8.     int i = 0;

  9.     printf("fgets:");
  10.     fgets(str, sizeof(str), stdin);
  11.     printf("str=%s\n", str);

  12.     printf("gets:");
  13.     gets(str);
  14.     printf("str=%s\n",str);

  15.     printf("getchar:");
  16.     while((ch = getchar()) != '\n')
  17.         str[i++] = ch;
  18.     printf("str=%s\n", str);

  19.     i = 0;
  20.     printf("getc:");
  21.     while((ch = getc(stdin)) != '\n')
  22.         str[i++] = ch;
  23.     printf("str=%s\n", str);
  24.     return 0;
  25. }

点击(此处)折叠或打开

  1. //fgets_fputs_cp.c
  2. //用fgets函数,实现copy函数的功能

  3. #include <stdio.h>

  4. void copy(char *src, char *dest)
  5. {
  6.     FILE *fp1, *fp2;
  7.     char buff[1024];

  8.     fp1 = fopen(src, "r");
  9.     fp2 = fopen(dest, "w");

  10.     if((fp1 == NULL) || (fp2 == NULL))
  11.         printf("%s/%s open failure\n", src, dest);

  12.     while((fgets(buff, sizeof(buff), fp1)) != NULL)
  13.         fputs(buff, fp2);
  14.     fclose(fp1);
  15.     fclose(fp2);
  16. }

  17. int main(int argc, char *argv[])
  18. {
  19.     if(argc != 3)
  20.         printf("Usage: copy src_file dest_file\n");
  21.     else
  22.         copy(argv[1], argv[2]);
  23.     return 0;
  24. }

点击(此处)折叠或打开

  1. //fprintf.c

  2. //stdout是行缓冲,stderr是无缓冲
  3. #include <stdio.h>

  4. int main(int argc, const char *argv[])
  5. {
  6.     //int val = fprintf(stderr,"%s\n", "hello");
  7.     //printf("val = %d\n", val);
  8.     printf("%s", "1234");
  9.     fflush(stdout);

  10.     return 0;
  11. }

点击(此处)折叠或打开

  1. //fscanf.c

  2. //fscanf与scanf的区别
  3. // scanf是从stdin中读数据
  4. // fscanf是从文件指针指向的文件中读数据(包括stdin)

  5. #include <stdio.h>

  6. int main(int argc, const char *argv[])
  7. {
  8.     int m, n;

  9.     fscanf(stdin, "%d %d", &m, &n);
  10.     printf("m = %d n= %d\n", m, n);
  11.     return 0;
  12. }

点击(此处)折叠或打开

  1. //sprintf.c

  2. //将format格式的变参列表,写入到指定字符串中

  3. #include <stdio.h>

  4. int main(int argc, const char *argv[])
  5. {
  6.     char buff[1024];
  7.     //sprintf(buff, "%s\n", "abc");
  8.     //printf("%s\n", buff);


  9.     int val = 100;
  10.     double d = 100.0;
  11.     char *s = "abc";
  12.     sprintf(buff, "%d%s%.1f", val, s, d);

  13.     printf("buff = %s\n", buff);
  14.     return 0;
  15. }

点击(此处)折叠或打开

  1. //sscanf.c

  2. //sscanf从指定字符串中读数据

  3. #include <stdio.h>

  4. int main(int argc, const char *argv[])
  5. {
  6.     int m, n;
  7.     char p[24] = {0};
  8.     char *s = "123 abc 45";
  9.     sscanf(s, "%d%s%d", &m, p, &n);
  10.     printf("m = %d p = %s n = %n\n", m, p, n);
  11.     return 0;
  12. }

点击(此处)折叠或打开

  1. //my_scanf.c
  2. #include <stdio.h>

  3. void my_scanf(int *num)
  4. {
  5.     char buff[8] = {0};
  6.     int i = 0;
  7.     char ch;

  8.     while((ch = getchar()) != '\n' && i < 8)
  9.     {
  10.         buff[i++] = ch;
  11.     }

  12.    *num = atoi(buff);
  13. }

  14. int main(int argc, const char *argv[])
  15. {
  16.     int num;

  17.     my_scanf(&num);

  18.     printf("num = %d\n", num);
  19.     return 0;
  20. }

点击(此处)折叠或打开

  1. //fread_fwrite_cp.c

  2. //使用fread和fwrite函数,实现copy函数功能
  3. //fread:读取指定块
  4. //fwrite:写指定块

  5. #include <stdio.h>
  6. #include <stdint.h>

  7. void copy(char *src, char *dest)
  8. {
  9.     FILE *srcp, *destp;
  10.     size_t n;
  11.     uint8_t arr[16];

  12.     srcp = fopen(src, "rb");
  13.     destp = fopen(dest, "wb");
  14.     if((srcp == NULL) || (destp == NULL))
  15.         printf("open file failure\n");

  16.     while((n = fread(arr, sizeof(uint8_t), 16,srcp)) != 0)
  17.         fwrite(arr, sizeof(uint8_t), n, destp);

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

  20. }

  21. int main(int argc, char *argv[])
  22. {
  23.     if(argc == 3)
  24.         copy(argv[1], argv[2]);
  25.     else
  26.         printf("Usage:copy src_file dest_file\n");
  27.     return 0;
  28. }

点击(此处)折叠或打开

  1. //fseek_ftell_rewind.c

  2. //使用fseek、ftell、rewind函数,求一个文件的长度

  3. #include <stdio.h>

  4. int main(int argc, const char *argv[])
  5. {
  6.     FILE *fp;
  7.     char ch;

  8.     fp = fopen(argv[1], "r");
  9.     if(fp == NULL)
  10.         printf("open failure\n");
  11.     
  12.     fseek(fp, 0, SEEK_END);
  13.     printf("%s: size= %ld\n", argv[1], ftell(fp));

  14.     fclose(fp);
  15.     return 0;
  16. }

点击(此处)折叠或打开

  1. //2_file_append.c

  2. //用fgets函数,实现将两个文件追加到第三个文件中

  3. #include <stdio.h>

  4. void append(char *src, char *dest, char *result)
  5. {
  6.     FILE *srcp, *destp, *resultp;
  7.     char buff[1024];

  8.     srcp = fopen(src, "r");
  9.     destp = fopen(dest, "r");
  10.     resultp = fopen(result, "a+");

  11.     if((srcp == NULL) || (destp == NULL) || (result == NULL))
  12.         printf("open file failure!\n");

  13.     while((fgets(buff, sizeof(buff), srcp)) != NULL)
  14.         fputs(buff, resultp);
  15.     while((fgets(buff, sizeof(buff), destp)) != NULL)
  16.         fputs(buff, resultp);
  17.     fclose(srcp);
  18.     fclose(destp);
  19.     fclose(resultp);
  20. }

  21. int main(int argc, char *argv[])
  22. {
  23.     if(argc != 4)
  24.         printf("Usage: append src dest result\n");
  25.     else
  26.         append(argv[1], argv[2], argv[3]);
  27.     return 0;
  28. }

点击(此处)折叠或打开

  1. //files_append.c

  2. //实现将argv[1]~argv[n-2]所指的文件,追加到argv[n-1]所指文件中

  3. #include <stdio.h>

  4. void append(int argc, char *argv[])
  5. {
  6.     FILE *resultp, *srcp;
  7.     char buff[1024];
  8.     int i;

  9.     resultp = fopen(argv[argc-1], "a");
  10.     for (i = 1 ; i < argc - 1; i++)
  11.     {
  12.         srcp = fopen(argv[i], "r");
  13.         if(srcp == NULL)
  14.             printf("open file %s failure\n", argv[i]);
  15.         while((fgets(buff, sizeof(buff), srcp)) != NULL)
  16.             fputs(buff, resultp);
  17.         fclose(srcp);
  18.     }

  19.     fclose(resultp);

  20. }

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

阅读(583) | 评论(0) | 转发(0) |
0

上一篇:链表 _0307

下一篇:文件操作_练习_0305

给主人留下些什么吧!~~