操作系统文件操作实验.docx

上传人:b****5 文档编号:2890705 上传时间:2022-11-16 格式:DOCX 页数:11 大小:81.38KB
下载 相关 举报
操作系统文件操作实验.docx_第1页
第1页 / 共11页
操作系统文件操作实验.docx_第2页
第2页 / 共11页
操作系统文件操作实验.docx_第3页
第3页 / 共11页
操作系统文件操作实验.docx_第4页
第4页 / 共11页
操作系统文件操作实验.docx_第5页
第5页 / 共11页
点击查看更多>>
下载资源
资源描述

操作系统文件操作实验.docx

《操作系统文件操作实验.docx》由会员分享,可在线阅读,更多相关《操作系统文件操作实验.docx(11页珍藏版)》请在冰豆网上搜索。

操作系统文件操作实验.docx

操作系统文件操作实验

 

xx工程学院

上机实验报告

课程名称:

操作系统

实验项目名称:

文件操作

学生班级:

学生学号:

学生姓名:

指导教师:

实验时间:

实验地点:

信息楼专业机房

实验成绩评定:

 

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

#include

#include

intNUM=0;

structstudent

{

charnum[20];//学号

charnam[20];//姓名

intscore;//成绩

structstudent*next;

};

typedefstructstudentStu;

typedefStu*STU;

voidSaveConf(STUhead);

voidMenu(STUhead);

voidChoose(STUhead);

voidLoadConf(STUhead);

voidCreate(STUhead);

voidInit(STU*head)//头节点初始化

{

(*head)=(STU)malloc(sizeof(Stu));

(*head)->next=NULL;

}

voidLoadConf(STUhead)//从文件加载信息至链表

{

inti=1;

FILE*fp;

STUnewstu;

STUp=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);

}

voidCreate(STUhead)//插入信息

{

STUnewstu;

STUp=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;

charflag;

getchar();

printf("是否继续插入信息(yorn):

");

scanf("%c",&flag);

if(flag=='y')

{

Create(head);

}

else

{

Choose(head);

}

}

voidSaveConf(STUhead)//保存信息到文件

{

FILE*fp;

STUp=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);

}

STUsearch(STUhead,char*s)//删除

{

STUp;

p=head->next;

while(p!

=NULL)

{

if(strcmp(s,p->num)==0)

{

returnp;

}

p=p->next;

}

returnp;

}

voidDelete(STUhead)

{

STUp;

STUq=head;

charflag;

charn[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("deletesuccess!

\n");

}

Choose(head);

}

voidConstant(STUhead)//查找

{

STUp=head;

charnumber[20];

intfind=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);

}

voidMenu(STUhead)

{

printf("\t********文件管理******\t\n");

printf("\t******1.插入信息******\t\n");

printf("\t******2.删除信息******\t\n");

printf("\t******3.查找信息******\t\n");

printf("\t******0.退出**********\t\n");

}

voidChoose(STUhead)

{

intchoice=0;

printf("\npleaseinputyourchoice:

");

scanf("%d",&choice);

switch(choice)

{

case1:

Create(head);break;

case2:

Delete(head);break;

case3:

Constant(head);break;

case0:

SaveConf(head);break;

default:

{

printf("输入有误,请重新输入!

\n");

Choose(head);

break;

}

}

}

intmain()

{

STUhead;

Init(&head);

LoadConf(head);

Menu(head);

Choose(head);

return0;

}

四、运行结果

创建新文件后的文本内容:

执行插入信息操作:

执行删除操作:

五、体会与提高

通过本次的实验设计,把教材中的理论知识转化为实践,在一定程度上加深了我对读者-写者这类经典的同步问题的理解,同时也提高了我的动手编程和独立思考的能力。

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

 

教师评语:

 

 

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 表格模板 > 调查报告

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1