linux 文件操作问题

来源:百度知道 编辑:UC知道 时间:2024/07/06 19:54:02
一、设计一个程序,要求判断“/etc/passwd”的文件类型。
使用st_mode属性,可以使用几个宏来判断:
S_ISLNK(st_mode)是否是一个连接,S_ISREG是否是一个常规文件,
S_ISDIR是否是一个目录,S_ISFIFO是否是一个FIFO文件,S_ISSOCK
是否是一个SOCKET文件;
并且要求打开文件“/etc/passwd”,判断它的最后一次访问时间。

二、设计一个程序,使用read函数从源文件读取数据,再用write函数写入到
目标文件,源文件名和目标文件名都由键盘输入。

三、设计一个程序,要求在“/mnt”目录下,打开名称为“usb”的文件,
如果该文件不存在,则创建此文件,如果已存在,把字符窗“usb作为优
盘设备文件”写入此文件后关闭。

四、设计一个程序,要求利用read函数读取系统文件“/ect/passwd”,
并在终端显示输出。
5编写一个程序,求2-n间的素数,n由键盘输入,循环变量分别从2到n、2到(int)sqrt(n),分别测出两个循环的所用时间。
6输入一个整型数组,再进行排序,然后键盘输入一个整数,用二分法进行查找。

我写第一个吧,仅供参考:

/*---------code-------------*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>

void what_is( int);

int main()
{

struct stat info;

if( stat("/etc/passwd", &info) == -1 )
perror("etc/passwd");

else
what_is( info.st_mode );

printf("last access time is %s\n", ctime(&info.st_mtime));

return 0;

}

void what_is( int mode )
{
if( S_ISDIR(mode) ) printf("/etc/passwd is Directory file.\n");
if( S_ISFIFO(mode) ) printf("/etc/passwd is Fifo file.\n");
if( S_ISLNK(mode) ) printf("/etc/passwd is link file.\n");
if( S_ISREG(mode) ) printf("/etc/passwd is regular file.\n");
if( S_ISSOCK(mode) ) pr