Chinaunix首页 | 论坛 | 博客
  • 博客访问: 83854
  • 博文数量: 14
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 159
  • 用 户 组: 普通用户
  • 注册时间: 2013-07-13 18:38
文章分类

全部博文(14)

文章存档

2013年(14)

分类: C/C++

2013-07-15 18:18:35


  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <errno.h>
  4. #include <string.h>
  5. struct stu
  6. {
  7.     char name[10];
  8.     int age;
  9. };

  10. int main()
  11. {
  12.     struct stu mystu[3];
  13.     FILE * fp;
  14.     extern int errno;
  15.     char file[]="/home/txt1.txt";
  16.     int i;

  17.     strcpy(mystu[0].name,"Jim");
  18.     mystu[0].age=22;
  19.     strcpy(mystu[1].name,"Jam");
  20.     mystu[1].age=43;
  21.     strcpy(mystu[2].name,"Lily");
  22.     mystu[2].age=18;

  23.     fp=fopen(file,"a+");
  24.     if(fp==NULL)
  25.     {
  26.         printf("can't open file %s.\n",file);
  27.         printf("errno:%d\n",errno);
  28.         printf("ERR :%s\n",strerror(errno));
  29.     }
  30.     else
  31.     {
  32.         printf("%s was opend .\n",file);

  33.     }
  34.     i=fwrite(mystu,sizeof(struct stu),3,fp);
  35.     printf("%d items was written .\n",i);
  36.     close(fp);
  37.     return 0;

  38. }
以上是fwrite的代码:

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <errno.h>
  4. #include <string.h>
  5. struct stu
  6. {
  7.     char name[10];
  8.     int age;
  9. }stu1[3];

  10. int main()
  11. {

  12.     FILE * fp;
  13.     extern int errno;
  14.     char file[]="/home/txt1.txt";
  15.     int i;

  16.     fp=fopen(file,"a+");
  17.     if(fp==NULL)
  18.     {
  19.         printf("can't open file %s.\n",file);
  20.         printf("errno:%d\n",errno);
  21.         printf("ERR :%s\n",strerror(errno));
  22.     }
  23.     else
  24.     {
  25.         printf("%s was opend .\n",file);

  26.     }
  27.     printf("%d\n",sizeof(struct stu));
  28.     for(i=0; i<3; i++)
  29.     {
  30.         fread(&stu1,sizeof(struct stu),3,fp);
  31.         printf("%s,%d\n",stu1[i].name,stu1[i].age);
  32.     }



  33. // printf("%d items was written .\n",i);
  34.     fclose(fp);
  35.     return 0;



  36. }

以上是fread的代码:
阅读(1496) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~