LINUX编程实验四 Linux的文件处理.docx

上传人:b****5 文档编号:11750398 上传时间:2023-03-31 格式:DOCX 页数:16 大小:106.52KB
下载 相关 举报
LINUX编程实验四 Linux的文件处理.docx_第1页
第1页 / 共16页
LINUX编程实验四 Linux的文件处理.docx_第2页
第2页 / 共16页
LINUX编程实验四 Linux的文件处理.docx_第3页
第3页 / 共16页
LINUX编程实验四 Linux的文件处理.docx_第4页
第4页 / 共16页
LINUX编程实验四 Linux的文件处理.docx_第5页
第5页 / 共16页
点击查看更多>>
下载资源
资源描述

LINUX编程实验四 Linux的文件处理.docx

《LINUX编程实验四 Linux的文件处理.docx》由会员分享,可在线阅读,更多相关《LINUX编程实验四 Linux的文件处理.docx(16页珍藏版)》请在冰豆网上搜索。

LINUX编程实验四 Linux的文件处理.docx

LINUX编程实验四Linux的文件处理

实验四Linux的文件处理

1.目的要求

(1)学习和掌握gcc等Linux的开发调试环境。

(2)学习并掌握Linux的文件操作。

(3)编写并实现“学生管理系统”程序。

2.实验内容

(1)文件底层系统调用实验

●使用vi将程序清单4-1和4-2的程序输入,并在当前目录下创建文件“file.in”和文件“file.out”,尽可能的使文件“file.in”大一些。

●利用gcc分别编译这两个程序,写出编译命令和执行结果;如果不成功,尝试利用gdb调试。

gcc–o4-14-1.c_____________________

gcc–o4-24-2.c_______________________________________________________________________________________________________________________

_________________________________________________________________________________________________

●仔细观察这两个程序,比较标准C的文件操作和Linux的系统调用open、read、write等的使用区别。

C文件系统调用fopen和fclose,fputc和fgetc进行对文件的读写,只需要指定文件名和读写方式,系统调用open,read,write等对文件进行读写,创建文件时要指定读写的权限,打开文件时,为了实现没有文件时创建文件,要加上特别的参数。

_________________________________________________________________________________________________________________________________________________________________________________________________________________________________________

●按照说明重新修改程序4-2,并使用time命令察看程序执行的时间效率上有何区别。

___每次只读写一个字符时用时多,因为每次都要启动磁盘,而利用数组的程序用时少,每次读写多个字符,启动磁盘的次数减少,所以用时少。

___________________________________________________________________________________________________________________________________________________________

●输入、编译并运行程序4-3和4-4,写出执行结果,并比较他们fseek和lseek在使用方法上的异同。

_

fseek使用的是FILE的指针,lseek使用的是文件描述符。

_____________________________________________________________________________________________________________________________________________

(2)目录信息的单层浏览

●使用vi将程序清单4-5和4-6程序输入。

●利用gcc分别编译这两个程序,写出编译命令和执行结果;如果不成功,尝试利用gdb调试。

_gcc–o4-54-5.c

gcc–o4-64-6.c

___________________________________________________________

____________________________________________________________________________________________

●仔细观察这两个程序,比较它们的区别。

 

(3)目录信息的完整浏览

●使用vi将程序清单4-7和4-8程序输入。

●利用gcc分别编译这两个程序,写出编译命令和执行结果;如果不成功,尝试利用gdb调试。

__gcc–o4-74-7.c

__gcc–o4-84-8.c

___________________________________________________________

____________________________

●仔细阅读、比较这两个程序,并写出目录浏览的算法描述。

_4-7程序不能传递参数,只能在程序内确定要浏览的目录路径,

4-8程序可以加参数_,如果没有参数,就默认为当前路径。

___________________________________________________________________4-7程序:

程序中提供浏览目录路径,调用opendir打开目录,然后进入,读取目录中文件,判断类型,如果是普通文件就输出文件名,如果是目录,就进入目录,循环调用函数,浏览文件

4-8程序:

运行程序时可以提供参数,然后浏览参数目录中的文件,如果没有提供就把当前目录当作参数,打开目录,进入目录,然后读取每项,判断文件类型,如果是目录,就循环调用浏览函数,如果是普通文件,就直接输出信息。

_________________________________________________________________________________________

(4)mmap、msync和munmap的使用

●使用vi将程序清单4-9程序输入。

●利用gcc编译程序,写出编译命令和执行结果;如果不成功,尝试利用gdb调试。

__gcc–o4-94-9.c

_

___________________________________________________________________________________________________________________________________________________________

仔细阅读程序,并画出程序流程图。

______________________________________________________________________________________________________________________________________________________________

(5)学习并分别使用标准C的文件操作函数和Linux的系统调用创建一个对学生基本信息进行操作(插入、修改和删除)的C程序,学生基本信息以结构体的形式存储在文件stu.info中。

structstu_info的定义如下:

structstu_info{

charstu_num[12];

charname[10];

shortintsex;/*0为女生,1为男生*/

charmobile_phone[12];

};

3.主要仪器设备及软件

(1)硬件:

计算机、网络

(2)软件:

VMWareworkstation、RedHat9.0

4.附录:

程序清单

(1)程序清单4-1

#include

#include

intmain(void)

{

charc;

FILE*in,*out;

if((in=fopen("file.in","r"))==NULL)

{

perror("fileopenerror!

");

exit(0);

}

out=fopen("file.out","w");

while((c=fgetc(in))!

=EOF)

fputc(c,out);

fclose(in);

fclose(out);

}

(2)程序清单4-2

#include

#include

#include

intmain()

{

//charblock[1024];

charc;

intin,out;

intnread;

in=open("file.in",O_RDONLY);

out=open("file.out",O_WRONLY|O_CREAT,S_IRUSR|S_IWUSR);

//将注释打开,并将两条语句的后一句注释掉,重新编译执行。

//while((nread=read(in,block,sizeof(block)))>0)

while((nread=read(in,&c,sizeof(c)))>0)

//write(out,block,nread);

write(out,&c,nread);

close(in);

close(out);

}

(3)程序清单4-3

#include

#include

#include

#include

#include

charbuf1[]="abcdefghij";

charbuf2[]="ABCDEFGHIJ";

voiderr_exit(char*err_s)

{

perror(err_s);

exit

(1);

}

intmain(void)

{

FILE*fp;

if((fp=fopen("hole.file","w"))==NULL)

err_exit("fileopenfail!

");

if(fwrite(buf1,sizeof(buf1),1,fp)!

=1)

err_exit("filewritebuf1error!

");

if(fseek(fp,40,SEEK_SET)==-1)

err_exit("fseekerror!

");

if(fwrite(buf2,strlen(buf2),1,fp)!

=1)

err_exit("filewritebuf2error!

");

fclose(fp);

}

(4)程序清单4-4

#include

#include

#include

#include

#include

charbuf1[]="abcdefghij";

charbuf2[]="ABCDEFGHIJ";

voiderr_exit(char*err_s)

{

perror(err_s);

exit

(1);

}

intmain(void)

{

intfd;

if((fd=open("hole.file",O_WRONLY|O_CREAT/*|O_APPEND,0644*/))==-1)

err_exit("fileopenfail!

");

if(write(fd,buf1,10)!

=10)

err_exit("filewritebuf1error!

");

if(lseek(fd,40,SEEK_SET)==-1)

err_exit("lseekerror!

");

if(write(fd,buf2,10)!

=10)

err_exit("filewritebuf2error!

");

}

(5)程序清单4-5

#include

#include

#include

intmain()

{

DIR*pDir=opendir(“MyDirectory”);

structdirent*pDirent;

structstatvStat;

if(pDir==NULL)

{

printf(“Can’topenthedirectory\”MyDirectory\”);

return1;

}

while((pDirent=readdir(pDir))!

=NULL)

{

lstat(pDirent->d_name,&vStat);

if(S_ISDIR(vStat.st_mode))

printf(“Directory:

%s\n”,pDirent->d_name);

else

printf(“File:

%s\n”,pDirent->d_name);

}

closedir(pDir);

return0;

}

(6)程序清单4-6

#include

#include

#include

intprintDir(char*path)

{

DIR*pDir=opendir(path);

structdirent*pDirent;

structstatvStat;

if(pDir==NULL)

{

printf(“Can’topenthedirectory:

%s”,path);

return-1;

}

while((pDirent=readdir(pDir))!

=NULL)

{

lstat(pDirent->d_name,&vStat);

if(S_ISDIR(vStat.st_mode))

printf(“Directory:

%s\n”,pDirent->d_name);

else

printf(“File:

%s\n”,pDirent->d_name);

}

closedir(pDir);

return0;

}

intmain()

{

char*dirPath;

printf(“pleaseinputbrowsedir’spath:

”);

scanf(“%s”,dirPath);

if(printDir(dirPath)==0)

return0;

else

return-1;

}

(7)程序清单4-7

/*Westartwiththeappropriateheadersandthenafunction,printdir,

whichprintsoutthecurrentdirectory.

Itwillrecurseforsubdirectories,usingthedepthparameterisusedforindentation.*/

#include

#include

#include

#include

#include

voidprintdir(char*dir,intdepth)

{

DIR*dp;

structdirent*entry;

structstatstatbuf;

if((dp=opendir(dir))==NULL){

fprintf(stderr,"cannotopendirectory:

%s\n",dir);

return;

}

chdir(dir);

while((entry=readdir(dp))!

=NULL){

lstat(entry->d_name,&statbuf);

if(S_ISDIR(statbuf.st_mode)){

/*Foundadirectory,butignore.and..*/

if(strcmp(".",entry->d_name)==0||

strcmp("..",entry->d_name)==0)

continue;

printf("%*s%s/\n",depth,"",entry->d_name);

/*Recurseatanewindentlevel*/

printdir(entry->d_name,depth+4);

}

elseprintf("%*s%s\n",depth,"",entry->d_name);

}

chdir("..");

closedir(dp);

}

/*Nowwemoveontothemainfunction.*/

intmain()

{

printf("Directoryscanof/home:

\n");

printdir("/home",0);

printf("done.\n");

exit(0);

}

(8)程序清单4-8

/*Westartwiththeappropriateheadersandthenafunction,printdir,

whichprintsoutthecurrentdirectory.

Itwillrecurseforsubdirectories,usingthedepthparameterisusedforindentation.*/

#include

#include

#include

#include

#include

voidprintdir(char*dir,intdepth)

{

DIR*dp;

structdirent*entry;

structstatstatbuf;

if((dp=opendir(dir))==NULL){

fprintf(stderr,"cannotopendirectory:

%s\n",dir);

return;

}

chdir(dir);

while((entry=readdir(dp))!

=NULL){

lstat(entry->d_name,&statbuf);

if(S_ISDIR(statbuf.st_mode)){

/*Foundadirectory,butignore.and..*/

if(strcmp(".",entry->d_name)==0||

strcmp("..",entry->d_name)==0)

continue;

printf("%*s%s/\n",depth,"",entry->d_name);

/*Recurseatanewindentlevel*/

printdir(entry->d_name,depth+4);

}

elseprintf("%*s%s\n",depth,"",entry->d_name);

}

chdir("..");

closedir(dp);

}

/*Nowwemoveontothemainfunction.*/

intmain(intargc,char*argv[])

{

char*topdir,pwd[2]=".";

if(argc!

=2)

topdir=pwd;

else

topdir=argv[1];

printf("Directoryscanof%s\n",topdir);

printdir(topdir,0);

printf("done.\n");

exit(0);

}

(9)程序清单4-9

/*WestartbydefiningaRECORDstructure

andthencreateNRECORDSversionseachrecordingtheirnumber.

Theseareappendedtothefilerecords.dat.*/

#include

#include

#include

#include

typedefstruct{

intinteger;

charstring[24];

}RECORD;

#defineNRECORDS(100)

intmain()

{

RECORDrecord,*mapped;

inti,f;

FILE*fp;

fp=fopen("records.dat","w+");

for(i=0;i

record.integer=i;

sprintf(record.string,"RECORD-%d",i);

fwrite(&record,sizeof(record),1,fp);

}

fclose(fp);

/*Wenowchangetheintegervalueofrecord43to143

andwritethistothe43rdrecord'sstring.*/

fp=fopen("records.dat","r+");

fseek(fp,43*sizeof(record),SEEK_SET);

fread(&record,sizeof(record),1,fp);

record.integer=143;

sprintf(record.string,"RECORD-%d",record.integer);

fseek(fp,43*sizeof(record),SEEK_SET);

fwrite(&record,sizeof(record),1,fp);

fclose(fp);

/*Wenowmaptherecordsintomemory

andaccessthe43rdrecordinordertochangetheintegerto243

(andupdatetherecordstring),againusingmemorymapping.*/

f=open("records.dat",O_RDWR);

mapped=

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

当前位置:首页 > 人文社科 > 法律资料

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

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