文件操作实验

文件操作实验

实验目标:

  1. 掌握Linux文件操作函数的使用

实验环境与工作条件:
Red Head Linux5.0以上操作系统

实验内容:

  1. 设计一个程序,要求打开文件“pass”,如果没有这个文件,新建此文件,权限设置为只有所有者有只读权限。
#include<unistd.h>
#include<stdlib.h>
#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
int main()
{
 int fd;
 if((fd=open("pass",O_CREAT))<0)
 {
  printf("not exist\n");
 }
 printf("create file pass,it is:%d\n",fd);
 chmod("pass",S_IRUSR|S_IWUSR);
 return 0;
}

在这里插入图片描述

  1. 设计一个程序,要求新建一个文件“hello”,利用write函数将“Linux下c软件设计”字符串写入该文件(如果用fwrite函数写入,应该怎么实现)。
#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,twrite;
 if((fd=open("./hello",O_CREAT|O_TRUNC|O_WRONLY,0777))<0)
 {
  printf("open file error");
  exit(1);
 }
 char str[]="the design of C software under Linux";
 if((twrite=write(fd,str,sizeof(str)))<0)
 {
  printf("write file error");
  exit(1);
 }
 close(fd);
 return 0;
}

在这里插入图片描述

  1. 设计一个程序,要求利用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,rtype,wtype;
 char str[30];
 if((fd=open("/etc/passwd",O_RDONLY))<0)
 {
  printf("read file error!");
  exit(1);
 }
 while((rtype=read(fd,str,30))>0)
 {
  if((wtype=write(STDOUT_FILENO,str,rtype))<0)
   printf("write file error");
 }
 close(fd);
      return 0;
}

在这里插入图片描述
在这里插入图片描述
4. 设计一个程序,要求打开文件“pass”,如果没有这个文件,新建此文件;读取系统文件“/etc/passwd”,把文件中的内容都写入“pass”文件。

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
int main()
{
 int fdsrc,fddes,nbytes,wtype;
 char str[30];
 if((fddes=open("pass",O_CREAT|O_TRUNC|O_WRONLY,0600))<0)
 {
  printf("open file pass error");
  exit(1);
 }
 if((fdsrc=open("/etc/passwd",O_RDONLY))<0)
 {
  printf("open file error");
  exit(1);
 }
 while((nbytes=read(fdsrc,str,30))>0)
 {
  if((wtype=write(fddes,str,30))<0)
  printf("write file error");
 }
        close(fdsrc);
 close(fddes);
}
 

在这里插入图片描述在这里插入图片描述
在这里插入图片描述
5. 设计一个程序,要求将10分别以十进制、八进制和十六进制输出(这里我采用了sprintf函数)。

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include<string.h>
int main()
{
 int x=10;
 char FX[20],PX[20],HX[20];
 sprintf(FX,"%x",x);
 sprintf(PX,"%o",x);
 sprintf(HX,"%d",x);
        write(1,PX,strlen(PX));
  printf("\n");
        write(1,FX,strlen(FX));
  printf("\n");
        write(1,HX,strlen(HX));
        printf("\n");
 return 0;
 
}

在这里插入图片描述
6. 设计一个程序,要求新建一个目录,预设权限为 dr–r--r–。

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
int main()
{
 system("mkdir exercise");
 chmod("exercise",0444);
 return 0;
}

在这里插入图片描述
7. 设计一个程序,要求用命令ls -l > list 建立一个文件,然后建立一个软链接“ls1”和一个硬链接为“ls2”,并查看两个链接文件和list文件。

#include<unistd.h>
#include<stdlib.h>
 
int main()
{
    system("ls -l > list");//>输出重定向,将当前目录下的内容输入到list中
    symlink("list","l1");
    link("list","l2");
    system("ls list -l");
    system("ls l1 -l");
    system("ls l2 -l");
    return 0;
}

在这里插入图片描述

发布了16 篇原创文章 · 获赞 1 · 访问量 180

猜你喜欢

转载自blog.csdn.net/weixin_44931542/article/details/105262219