文档介绍:《Linux系统高级编程》实验指导书
实验五
实验题目:Linux环境下文件I/O操作
实验目的:熟悉并掌握Linux环境下文件的属性、不带缓存的文件I/O操作、带缓存的流文件I/O操作、特殊文件的操作。
实验类型:综合
实验要求:必修
仪器设备:计算机
实验内容、方法、步骤:
一、Linux系统文件的属性
设计程序,满足如下要求:
读懂并编写测试本节所有示例程序,然后按照要求完成如下程序。
1、设计一个程序,要求列出当前目录下的文件信息,以及系统“/etc/inittab”和“/dev/sda2”的文件信息。
: system(“ls –l”);
#include<stdio.h>
#include<>
int main()
{
int n;
printf("information\n");
ﻩn=system("ls -l");
ﻩprintf("/etc/inittab:\n");
ﻩn=system("ls -l /etc/inittab");
ﻩprintf("/dev/sda2:\n");
ﻩn=system("ls -l /dev/sda2");
ﻩreturn 0;
}
2、设计一个程序,要求把系统中的“/etc”目录下的group文件权限,设置成文件所有者可读可写,所有其他用户为只读权限。
:chmod(“/etc/group”,S_IRUSR|S_IWUSR|…..)
#include<sys/>
#include<sys/>
int main()
{
ﻩchmod("/etc/group",S_IRUSR|S_IWUSR|S_IROTH|S_IRGRP);
ﻩreturn 0;
}
3、设计一个程序,要求新建一个目录,预设权限为 ---x--x--x。777-u=111
u=666
: umask
#include<stdio.h>
#include<>
#include<sys/>
#include<sys/>
int main()
{
ﻩmode_t new,old;
ﻩnew=0666;
ﻩold=umask(new);
ﻩprintf("%o\n",new);
ﻩprintf("%o\n",old);
ﻩsystem("mkdir luyouqing");
ﻩsystem("ls -l luyouqing");
}
4、设计一个程序,应用系统函数stat获取系统中“/etc”目录下的group文件的大小。
:struct stat s; //s.st_size
:stat(“/etc/group”,&s);
#include<unistd.h>
#include<sys/stat.h>
#include<>
int main()
{
ﻩstruct stat buf;
ﻩstat("/etc/group",&buf);
ﻩprintf("/etc/group size :%d\n",);
ﻩreturn 0;
}