操作系统文件操作实验_第1页
操作系统文件操作实验_第2页
操作系统文件操作实验_第3页
操作系统文件操作实验_第4页
操作系统文件操作实验_第5页
已阅读5页,还剩4页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、上机实验报告南京工程学院上机实 验 报 告 课 程 名 称: 操作系统 实验项目名称: 文件操作 学生班级: 学生学号: 学生姓名: 指导教师: 实 验 时 间: 实 验 地 点: 信息楼专业机房 实验成绩评定: 2016-2017-1学期1- 3 -一、实验目的及内容在掌握文件的概念和文件管理功能后,通过实验进一步了解文件的组织结构以及常规操作,从而了解文件的实际应用,为大量信息处理问题提供一种实用有效的管理模式。内容:创建一个新文件,文件内容为本班所有同学的学号、姓名、操作系统课程成绩,要求采用有格式的存储格式;文件建立之后,能够对文件进行插入、删除、查找等操作。二、实验相关知识简介文件系

2、统提供给用户程序的一组系统调用,如文件的建立、打开、关闭、撤消、读、写和控制等,通过这些系统调用用户能获得文件系统的各种服务。不同的系统提供给用户不同的对文件的操作手段,但所有系统一般都提供以下关于文件的基本操作:1对整体文件而言(1)打开(open)文件,以准备对该文件进行访问。(2)关闭(close)文件,结束对该文件的使用。(3)建立(create)文件,构造一个新文件。(4)撤消(destroy)文件,删去一个文件。(5)复制(copy)文件,产生一个文件副本。2对文件中的数据项而言(1)读(read)操作,把文件中的一个数据项输入给进程。(2)写(write)操作,进程输出一个数据项

3、到文件中去。(3)修改(update)操作,修改一个已经存在的数据项。(4)插入(insert)操作,添加一个新数据项。(5)删除(delete)操作,从文件中移走一个数据项。 三、设计思路及关键程序代码分析#include <stdio.h> #include <string.h> #include <stdlib.h> int NUM = 0; struct student char num20; / 学号 char nam20; / 姓名 int score; / 成绩 struct student * next; ; typedef struct s

4、tudent 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 ne

5、wstu; 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 %dn",newstu->num,newstu->nam,&newstu->

6、;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(&qu

7、ot;%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

8、 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,&qu

9、ot;%s %s %dn",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 n20; print

10、f("请输入需要删除学生的学号: "); scanf("%s",&n); p = search(head,n); if(p = NULL) getchar(); printf("您输入的学号不存在,请重新输入: "); scanf("%c",&flag); Delete(head); else getchar(); printf("%s %s %dn",p->num,p->nam,p->score); q = head; while(q->next !=

11、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 number20;int find=0; printf("请输入需要查找学生的学号:"); scanf("%s",number); while(p != NULL) if(strcmp(number

12、,p->num) = 0) printf("查找结果如下:n"); printf("%s %s %dn",p->num,p->nam,p->score); find=1; p = p->next; if(find=0)printf("您输入的学号不存在!"); getchar(); Choose(head); void Menu(STU head) printf("t*文件管理*tn"); printf("t*1.插入信息*tn"); printf("t*

13、2.删除信息*tn"); printf("t*3.查找信息*tn"); printf("t*0.退出*tn"); 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); Lo

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论