c语言每周500字程序员

C语言的文件(篇章之三)

2016-06-17  本文已影响63人  遮天的龙

二进制文件操作(以及一个小项目)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int fwrite(void *buffer,int num_bytes,int count ,FILE *fp);
int fread(void *buffer,int num_bytes,int count ,FILE *fp) ;
返回值 int  成功,返回读/写的字段数;出错或文件结束,返回0;

特别注意返回值陷阱: 理解最小单元与读取个数的实际意义。
fread(起始地址,最小单元,读取个数,文件句柄) > 0 ;判断文件读取结束
读写结构体是长项。
fread 、fwrite 对文本的标志性字符不敏感。

一个简单的学生成绩系统

typedef struct student     //构造结构体
{
  int num;            
  char name[30];
  char sex;
  float math;
  float english;
  float chinese;
}Stu;

typedef struct node         //链表的头节点
{
  Stu data;
  struct node * next;
}Node;

void init()                  //初始化数据
{
  Stu stu[] = {
      { 01, "xiaoming", 'm', 100, 100,100 },
      { 02, "xiaohong", 'w', 100, 100,100 },
      { 03, "xiaodong", 'm', 100, 100,100 }
  };
  FILE* fp = fopen("stu.data", "wb+");
  if (NULL == fp)
      exit(1);

  fwrite((void*)stu, sizeof(stu), 1, fp);
  fclose(fp);
}

Node *createListFromFile()      //读写文件到链表
{
  Node *head = (Node*)malloc(sizeof(Node));
  head->next = NULL;
  FILE* fp = fopen("stu.data", "rb");
  if (NULL == fp)
      return NULL;

  Node * cur = (Node*)malloc(sizeof(Node));
  while (fread((void*)&cur->data, sizeof(Stu), 1, fp) > 0)  //核心语句
  {
      cur->next = head->next;     //头插法
      head->next = cur;

      cur = (Node*)malloc(sizeof(Node));
  }
  free(cur);
  return head;
}

void displayListOfStu(Node *head)    //界面
{
  head = head->next;

  while (head)
  {

      printf("num = %d name = %s, sex = %c,Math = %0.2f,Eng = %0.2f,Chi = %0.2f\n",
        head->data.num, head->data.name, head->data.sex,
        head->data.math, head->data.english, head->data.chinese);

      head = head->next;
  }
}

void addListOfStu(Node *head)   //添加界面
{
  Node *node = (Node*)malloc(sizeof(Node));

  printf("请输入学号:"); scanf("%d", &node->data.num);
  printf("请输入姓名:"); scanf("%s", node->data.name); getchar();
  printf("请输入性别:"); scanf("%c", &node->data.sex);
  printf("请输入数学:"); scanf("%f", &node->data.math);
  printf("请输入英语:"); scanf("%f", &node->data.english);
  printf("请输入语文:"); scanf("%f", &node->data.chinese);

  node->next = head->next;
  head->next = node;

}
void saveList2File(Node *head)       
{
  FILE*fp = fopen("stu.data", "wb");
  if (fp == NULL)
      return exit(1);
  head = head->next;
  while (head)
  {
      fwrite((void*)&head->data, sizeof(Stu), 1, fp);
      head = head->next;
  }
  fclose(fp);
}

int main(void)   //主函数
{

  init();
  Node *head = createListFromFile();

  displayListOfStu(head);

  addListOfStu(head);
  displayListOfStu(head);

  saveList2File(head);

  return 0;
}

C文件的难点在于两个地方:
(1)文件的结尾判断:
a、对于文件文件读写的时候,一般用-1为最佳
b、对于二进制读写的时候,不要判断-1,因为-1对于二进制是有意义,所有一般要用feof判断
(2)换行符的处理:
a、对于文本文件读写的时候(用这两个标志位r,w),一般把\n处理成\r\n
b、对于二进制读写的时候(用rb,wb),则对于\n不会处理成\r\n


通用的存取文件的规则:
(1)存取文本文件:标志位:r,w 函数用:fgetc,getc,fputc,putc,fgets,fputs
(2)存取二进文件:标志位:rb,wb 函数用:fwrite,fread

上一篇下一篇

猜你喜欢

热点阅读