目的
- 熟悉文件描述符操作文件的函数;
- 掌握非阻塞文件操作应用;
- 掌握文件锁应用;
- 掌握目录文件、符号链接文件的操作。
内容
- 教材第5章末,213页,1,2,3,4,7题。
- 设计一个程序,要求打开文件“pass”,如果没有这个文件,新建此文件,权限设置为只有所有者有只读权限。
-
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> int main() { int fd; if((fd=open("pass",O_CREAT)<0)) { printf("file is not exist.\n"); } printf("createFile pass,fd:%d\n",fd); chmod("pass",S_IRUSR); return 0; }
2.设计一个程序,要求新建一个文件“hello”,利用write函数将“Linux下C软件设计”字符串写入该文件。
-
#include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <string.h> int main() { int fd,w; if((fd=open("hello",O_TRUNC|O_WRONLY,0666))<0) { printf("打开文件出错"); return 1; } char str[]="Linux下C软件设计"; if((w=write(fd,str,sizeof(str)))<0) { printf("写入文件出错"); return 1; } close(fd); return 0; }
3.设计一个程序,要求利用read函数读取系统文件“/etc/passwd”,并在终端中显示输出
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
int main()
{
int fd,r,w;
char str[50];
if((fd=open("/etc/passwd",O_RDONLY))<0)
{
printf("读取文件失败!");
return 1;
}
while((r=read(fd,str,50))>0)
{
if((w=write(STDOUT_FILENO,str,r))<0)
printf("写入文件出错");
}
close(fd);
return 0;
}
设计一个程序,要求打开文件“pass”,如果没有这个文件,新建此文件,在读取系统文件“/etc/passwd”,把文件的内容都写入“pass”文件。
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main()
{
int fdsrc,fddes,cnt,w;
char str[30];
if((fddes=open("pass",O_CREAT|O_TRUNC|O_WRONLY,0600))<0)
{
printf("打开文件pass失败!");
return 1;
}
if((fdsrc=open("/etc/passwd",O_RDONLY))<0)
{
printf("打开文件失败!");
return 1;
}
while((cnt=read(fdsrc,str,30))>0)
{
if((w=write(fddes,str,30))<0)
printf("写入文件失败");
}
close(fdsrc);
close(fddes);
return 0;
}
问题:一直没有找到为什么文件pass一直打开失败?
⑦ 设计一个程序,要求为“/bin/ls”文件建立一个软链接“ls1”和一个硬链接“ls2”,并查看两个链接文件和“/bin/ls”文件。
#include <stdlib.h>
#include <unistd.h>
int main()
{
symlink("/bin/ls","ls1");
link("/bin/ls","ls2");
system("ls -l /bin/ls");
system("ls -l ls1");
system("ls -l ls2");
return 0;
}
- 编程实现ls的基本功能,并实现-l参数功能;如:
- 若没指定参数则显示当前目录的所有可见文件;
- 若指定参数,参数指定问文件显示该文件信息,若为目录则显示该目录中的的文件信息;
- -l参数格式同ls -l;
- 指定参数可以是多个;
#include<stdio.h> #include<unistd.h> #include<stdlib.h> #include<sys/types.h> #include<sys/stat.h> #include<string.h> #include<time.h> #include<pwd.h> #include<grp.h> void filebuf(mode_t mode,char *rs){ //判断文件类型以及所有者,组和其他的权限; if(S_ISDIR(mode)){ rs[0]='d'; } else if(S_ISREG(mode)){ rs[0]='-'; } else if(S_ISFIFO(mode)){ rs[0]='p'; } else if(S_ISLNK(mode)){ rs[0]='l'; } else if(S_ISBLK(mode)){ rs[0]='b'; } else if(S_ISSOCK(mode)){ rs[0]='s'; } else if(S_ISCHR(mode)){ rs[0]='c'; } if(mode & S_IRUSR) rs[1]='r'; else rs[1]='-'; if(mode & S_IWUSR) rs[2]='w'; else rs[2]='-'; if(mode & S_IXUSR) rs[3]='x'; else rs[3]='-'; if(mode & S_IRGRP) rs[4]='r'; else rs[4]='-'; if(mode & S_IWGRP) rs[5]='w'; else rs[5]='-'; if(mode & S_IXGRP) rs[6]='x'; else rs[6]='-'; if(mode & S_IROTH) rs[7]='r'; else rs[7]='-'; if(mode & S_IWOTH) rs[8]='w'; else rs[8]='-'; if(mode & S_IXOTH) rs[9]='x'; else rs[9]='-'; rs[10]='\0'; } int main(int argc, char *argv[]) { struct stat fst; struct tm *mytime=(struct tm *)malloc(sizeof(struct tm)); char rs[12]; if(argc!2){ fprintf(stderr,"Usage: %s <pathname>\n",argv[0]); exit(EXIT_FAILURE); } if(stat(argv[1],&fst)==-1) { perror("stat"); exit(EXIT_FAILURE); } filebuf(fst.st_mode,rs); printf("%s",rs); //输出文件类型与权限信息; printf(" %d",fst.st_nlink);//输出文件的硬链接数; printf(" %s",getpwuid(fst.st_uid)->pw_name);//输出所属用户名; printf(" %s",getgrgid(fst.st_gid)->gr_name);//输出用户所在组; printf(" %1d",fst.st_size);//输出文件大小; mytime=localtime(&fst.st_mtime);//获取文件修改时间; printf("%d-%02d-%02d%02d:%02d",mytime->tm_year+1900,mytime-> tm_mon+1,mytime->tm_mday,mytime->tm_hour,mytime->tm_min); printf(" %s",argv[1]);//输出文件名; printf("\n"); return 0; }