《Linux操作系统》实验十二-标准IO、文件与目录.pdf
《Linux操作系统》
实验报告
实验十二:标准IO、文件
与目录
一、实验目的
(1)掌握Linux/Unix系统的标准I/O编程方法;
(2)掌握文件与目录属性编程控制方法;
(3)掌握临时文件的使用;
(4)了解目录编程与管理。
二、实验环境
一台装有Windows操作系统和Linux机系统的微机或服务器。
三、实验内容与实验过程及分析(写出详细的实验步骤,并分析
实验结果)
示例1:
#includestdio.h
main(){
charname[L_tmpnam],line[100];
FILE*fp;
tmpnam(name);//2ndtmpnam
//Disp2ndtmpnam
if((fp=tmpfile())==NULL)//createtempfile
perror(name);
//writetotmpfile
rewind(fp);//totheBeginning
if(fgets(line,sizeof(line),fp)==NULL)if(fgets(line,sizeof(line),fp)==NULL)
fputs(line,stdout);
}
示例2:文件或目录属性操作
#includesys/types.h
#includesys/stat.h
main(intargc,char*argv[])
{
inti;
structstatbuf;
char*ptr;
for(i=1;iargc;i++){
if(lstat(argv[i],buf)0){
continue;
}
ifif
#ifdefS_ISLNK
#endif
#ifdefS_ISSOCK
#endif
}
exit(0);
}
示例3:修改示例2,使之具有ls功能
#includesys/types.h
#includesys/stat.h
main(intargc,char*argv[])
{
inti;
structstatbuf;
char*ptr;
for(i=1;iargc;i++){
if(lstat(argv[i],buf)0){
continue;
}
ifif
#ifdefS_