首页 > 解决方案 > 内核模块编程 - 如何删除用户空间文本文件中的所有数据

问题描述

我知道这样做不值得,但我必须这样做。从该文件读取后,我想从名为 file1.text 的特定文本文件中删除信息,我该如何在我的模块中执行此操作。非常感谢所有的帮助者。



    static int checkDOS(char *name)
    {

       //get the path to this file
       char path[2000];
       strncat(path,"/home/daniel/count/should/",1000);
       strncat(path,name,1000);
       struct file * file_dos;

       //Open this file
       file_dos=filp_open(path,O_RDONLY,0);
       char count[1];//save the first character of file
       
       //check if the file is exists
       if(IS_ERR(file_dos) == 0)
       {
          
          file_read(file_dos,0,count,1);//Read this file
          if(count[0]=='0')
          {
            return 0;
          }


          char count2[200];//Stores the number listed in the file 
          filp_close(file_dos,NULL);//close the this text file
          file_sync(file_dos);

         //Takes the character stored in the count variable converts 
          // it to a number and then subtracts one from it, then 
          
          file_dos=filp_open(path,O_WRONLY, 0);
          long count_dos;
          kstrtol(count,0,&count_dos);
          count_dos=count_dos-1;

          //converts it to a string, then writes in a file
          sprintf(count2, "%d", count_dos);
          file_write(file_dos,1,count2,1);
          filp_close(file_dos,NULL);
          return 1;
      
       }
       return 0;

     }  

标签: clinux-kernelkernelkernel-module

解决方案


推荐阅读