操作系统课程设计三、熟悉linux文件系统调用

操作系统课程设计三、熟悉linux文件系统调用#

实验内容###

使用文件系统调用编写一个文件工filetool使其具有以下功能:

1.创建新文件

2.写文件

3.读文件

4.修改文件权限

5.查看当前文件权限

0.退出

提示用户输入功能号,并根据用户输入的功能选择相应的功能。
文件按可变记录文件组织,具体记录内容自行设计。

实验功能及设计思路###

程序功能:

  1. 实现文件调用系统,实现linux文件系统调用
  2. 支持输入文件名,实现打开或创建文件(如果该文件不存在)
  3. 支持文件写入,在打开文件或者新建文件之后,用户可以如何任何内容加入到当前文件的后面。
  4. 支持读文件功能,展现文件信息
  5. 支持修改权限文件功能。例如777表示所有者,所属者和其他人都存在读写执行的权限。
  6. 支持查看文件类型,格式为-rwxrwxrwx-
  7. 支持退出当前文件,重新打开或者创建新的文件。

设计思路:

  1. 了解linux文件调用的基本知识。
    用户在针对文件进行操作之前时一定要先打开它,这是由于系统需要根据用户提供的参数来查找文件的目录项,并由目录项找到文件的磁盘 i 结点,再将它调到内存 i 结点,才能建立用户进程与该文件之间的联系。
    同样,在文件操作完毕后要关闭文件,以切断用户进程与文件的联系,释放相关资源。
Open 系统调用
int open(const char *path, int flags);
int open(const char *path, int flags,mode_t mode);

一般情况下使用两个参数的格式,只有当打开的文件不存在时才使用 3 个参数的格式。参数:
Path 指向所要打开的文件的路径名指针。
int close(int fd)
Fd 为打开文件时系统返回的文件标识符
系统执行该系统调用时,根据 fd 值在该进程的进程打开文件表中找到 fd 标识,根据指针找到系统打开文件表,再找到内存 i 结点表中相应的 i 结点,对其 i_count 进行减 1 操作,
然后释放系统打开文件表中的表项和进程打开文件表的表项。返回结果:调用成功返回 0
2. 对于打开文件,首先输入文件名,如果文件存在,就直接打开,如果文件不存在则新建文件。给出提示信息。
3. 对于写文件,首先判断文件是否打开,否则必须调用功能打开文件,然后使用过write(fd, buffer, strlen(buffer))系统函数写入文件。
4. 对于修改文件权限,首先判断文件是否打开,否则必须调用功能打开文件。输入格式为777(或者其他权限),首先对模式分解,调用系统函数chmod。修改是否成功给出提示信息。
5. 对于读文件,首先判断文件是否打开,否则必须调用功能打开文件。清空缓冲区,打印到界面。
6. 查看文件权限,首先判断文件是否打开,否则必须调用功能打开文件。调用execv系统功能函数。
7. 在实现每一个函数,采用功能聚集,每一个函数实现对于的功能。
8. 在主函数中,设计程序界面,实现用户调用。

源代码##

#include <iostream>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<unistd.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
using namespace std;
char filename[50];
int fd;
bool opened=false;
char* pargv[4] = { "ls", "-l", NULL, NULL };
void create_open_file(){
      printf("请输入文件名
");
      scanf("%s",filename);
      //读写方式打开,不存在就新建
      umask(0000);
      fd=open(filename,O_RDWR |  O_CREAT,0666);
      pargv[2] = (char*)malloc(50);
      strcpy(pargv[2], filename);
      if(fd<0){
            printf("打开失败
");
      }
      else{
         opened=1;
         printf("打开成功
");
      }
}
void write_file(){
      if(opened==0){
           printf("首先需要打开文件
");
           return;
      }
      char buffer[1<<10];
      printf("请输入文件内容
");
      scanf("%s",buffer);
      int returnnum=write(fd, buffer, strlen(buffer));
      if(returnnum!=-1){
            printf("写入成功!
");
      }else{
            printf("写入失败!
");
      }
}
void  mode_file(){
      if(opened==0){
            printf("首先需要打开文件
");
           return;
      }
      int mode;
      printf("输入新的模式
");
      scanf("%d",&mode);
   int mode_u = mode / 100;
    int mode_g = mode / 10 % 10;
    int mode_o = mode % 10;
    mode = (mode_u * 8 * 8) + (mode_g * 8) + mode_o;   //八进制转换
      int returnnum=chmod(filename, mode);
      if(returnnum==-1){
            printf("改变模式失败!
");
      }
      else{
             printf("改变模式成功!
");
      }
//      close(fd);
//      opened=0;
}
void read_file(){
      if(opened==0){
          printf("首先需要打开文件
");
          return;
      }
      char buffer[1<<10];
      memset(buffer,0,sizeof(buffer));
      //lseek() 定位一个已经打开的文件
      lseek(fd, 0, SEEK_SET);
      int returnnum=read(fd,buffer,1024);
      if(returnnum!=-1){
            printf("文件内容为:
");
            printf("%s
",buffer);
      }else{
           printf("读取失败
");
      }
//      close(fd);
//      opened=0;
}
void view_mode(){
      if(opened==0){
            printf("首先需要打开文件
");
           return;
      }
      printf("%文件权限为");//execv会停止执行当前的进程
      int returnnum = execv("/bin/ls", pargv);
      if(returnnum==-1){
            printf("查看失败
");
      }
      else{
           printf("文件权限为:
");
      }
//      close(fd);
//      opened=0;
}
int main()
{
   printf("**********文件系统调用程序*********
");
    int choice;
    while(1){
      printf("
请选择您的选项
");
      printf("1.新建(不存在)或者打开文件
");
      printf("2.读文件
");
      printf("3.写文件
");
      printf("4.给文件权限修改
");
      printf("5.查看文件权限
");
      printf("6.关闭文件
");
      scanf("%d",&choice);
      switch(choice){
       case 1:create_open_file();
                  break;
       case 2:read_file();
                  break;
       case 3:write_file();
                  break;
       case 4:mode_file();
                  break;
       case 5:view_mode();
                  break;
       case 6:close(fd);
              opened=0;
              break;
       default:
            printf("input error!!");
            break;

      }
    }
    return 0;
}

原文地址:https://www.cnblogs.com/gzr2018/p/11913844.html