当前位置:文档之家› 操作系统文件操作实验

操作系统文件操作实验

南京工程学院上机实验报告课程名称:操作系统实验项目名称:文件操作学生班级:学生学号:学生姓名:指导教师:实验时间:实验地点:信息楼专业机房实验成绩评定:2016-2017-1学期一、实验目的及内容在掌握文件的概念和文件管理功能后,通过实验进一步了解文件的组织结构以及常规操作,从而了解文件的实际应用,为大量信息处理问题提供一种实用有效的管理模式。

内容:创建一个新文件,文件内容为本班所有同学的学号、姓名、操作系统课程成绩,要求采用有格式的存储格式;文件建立之后,能够对文件进行插入、删除、查找等操作。

二、实验相关知识简介文件系统提供给用户程序的一组系统调用,如文件的建立、打开、关闭、撤消、读、写和控制等,通过这些系统调用用户能获得文件系统的各种服务。

不同的系统提供给用户不同的对文件的操作手段,但所有系统一般都提供以下关于文件的基本操作:1.对整体文件而言(1)打开(open)文件,以准备对该文件进行访问。

(2)关闭(close)文件,结束对该文件的使用。

(3)建立(create)文件,构造一个新文件。

(4)撤消(destroy)文件,删去一个文件。

(5)复制(copy)文件,产生一个文件副本。

2.对文件中的数据项而言(1)读(read)操作,把文件中的一个数据项输入给进程。

(2)写(write)操作,进程输出一个数据项到文件中去。

(3)修改(update)操作,修改一个已经存在的数据项。

(4)插入(insert)操作,添加一个新数据项。

(5)删除(delete)操作,从文件中移走一个数据项。

三、设计思路及关键程序代码分析#include <stdio.h>#include <string.h>#include <stdlib.h>int NUM = 0;struct student{char num[20]; // 学号char nam[20]; // 姓名int score; // 成绩struct student * next;};typedef struct student Stu;typedef Stu * STU;void SaveConf(STU head);void Menu(STU head);void Choose(STU head);void LoadConf(STU head);void Create(STU head);void Init(STU *head) //头节点初始化{(*head) = (STU)malloc(sizeof(Stu));(*head)->next = NULL;}void LoadConf(STU head) //从文件加载信息至链表{int i = 1;FILE *fp;STU newstu;STU p= head;fp = fopen("text.txt","r+");if(fp == NULL){printf("文件不存在!已为您创建新文件!\n");fp = fopen("text.txt","a+");}while(i > 0){newstu = (STU)malloc(sizeof(Stu));i = fscanf(fp,"%s %s %d\n",newstu->num,newstu->nam,&newstu->score);if(i == -1){free(newstu);newstu = NULL;break;}p = head;while(p->next != NULL){p = p->next;}p->next = newstu;newstu->next = NULL;}p = NULL;fclose(fp);}void Create(STU head) //插入信息{STU newstu;STU p = head;newstu = (STU)malloc(sizeof(Stu));printf("请输入学号:");scanf("%s",newstu->num);printf("请输入姓名:");scanf("%s",newstu->nam);printf("请输入成绩:");scanf("%d",&newstu->score);while(p->next != NULL){p = p->next;}p->next = newstu;newstu->next = NULL;char flag;getchar();printf("是否继续插入信息(y or n):");scanf("%c",&flag);if(flag=='y'){Create(head);}else{Choose(head);}}void SaveConf(STU head) //保存信息到文件{FILE *fp;STU p = head->next;fp = fopen("text.txt","w");if(fp == NULL){printf("打开文件失败!\n");return;}while(p != NULL){fprintf(fp,"%s %s %d\n",p->num,p->nam,p->score); //写入数据到文件中p = p->next;}fclose(fp);}STU search(STU head,char *s) //删除{STU p;p = head->next;while(p != NULL){if(strcmp(s,p->num) == 0){return p;}p = p->next;}return p;}void Delete(STU head){STU p;STU q = head;char flag;char n[20];printf("请输入需要删除学生的学号: ");scanf("%s",&n);p = search(head,n);if(p == NULL){getchar();printf("您输入的学号不存在,请重新输入: ");scanf("%c",&flag);Delete(head);}else{getchar();printf("%s %s %d\n",p->num,p->nam,p->score);q = head;while(q->next != p){q = q->next;}q->next = p->next;p->next = NULL;free(p);p = NULL;printf("delete success!\n");}Choose(head);}void Constant(STU head) //查找{STU p = head;char number[20];int find=0;printf("请输入需要查找学生的学号:");scanf("%s",number);while(p != NULL){if(strcmp(number,p->num) == 0){printf("查找结果如下:\n");printf("%s %s %d\n",p->num,p->nam,p->score);find=1;}p = p->next;}if(find==0)printf("您输入的学号不存在!");getchar();Choose(head);}void Menu(STU head){printf("\t********文件管理******\t\n");printf("\t******1.插入信息******\t\n");printf("\t******2.删除信息******\t\n");printf("\t******3.查找信息******\t\n");printf("\t******0.退出**********\t\n");}void Choose(STU head){int choice = 0;printf("\nplease input your choice: ");scanf("%d",&choice);switch(choice){case 1:Create(head);break;case 2:Delete(head);break;case 3:Constant(head);break;case 0:SaveConf(head);break;default:{printf("输入有误,请重新输入!\n");Choose(head);break;}}}int main(){STU head;Init(&head);LoadConf(head);Menu(head);Choose(head);return 0;}四、运行结果创建新文件后的文本内容:执行插入信息操作:执行删除操作:五、体会与提高通过本次的实验设计,把教材中的理论知识转化为实践,在一定程度上加深了我对读者-写者这类经典的同步问题的理解,同时也提高了我的动手编程和独立思考的能力。

虽然在分析问题的过程中,遇到了很多的疑惑与不解,但同时掌握了很多进程同步的知识教师评语:。

相关主题