0

I know it's not worth doing but I have to. I want to delete information from a specific text file that called file1.text after i read from this file , How can i do that in my module. Many thanks to all the helpers.



    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;

     }  
daniel
  • 25
  • 7
  • 2
    What about opening the file for **write** and closing it? This way works in user space: https://stackoverflow.com/questions/4815251/how-do-i-clear-the-whole-contents-of-a-file-in-c. In the kernel, a file could be opened with `filp_open` function. About working with files in the Linux kernel see e.g. that question: https://stackoverflow.com/questions/1184274/read-write-files-within-a-linux-kernel-module. – Tsyvarev May 18 '21 at 11:45
  • it does not work for me – daniel May 19 '21 at 08:32
  • 1
    "it does not work" is a silly description of the problem. What exact code have you tried? Does it compiled successfully? Does it run without faults? What exactly do you observe which doesn't corresponds to your expectations? (E.g. the code is executed, but content of the file remains unchanged). Please, update your question with a detailed description of your attempt. – Tsyvarev May 19 '21 at 09:03
  • The code is executed, but content of the file remains unchanged – daniel May 19 '21 at 11:22
  • 1
    Please, add the code, which you have tried, into the question post itself. – Tsyvarev May 19 '21 at 11:48
  • Thank you so much for helping me, and I uploaded the code – daniel May 19 '21 at 13:05
  • Convert pictures to texts. – 0andriy May 21 '21 at 20:46
  • I uploaded the code – daniel May 26 '21 at 08:34
  • You should be able to open the file for reading, then unlink it (delete its name from the file system), then create a new file with the same name. The original file you opened can still be read by the file handle you already got; and the original file contents should be deleted when there's no more file handles (when you close the file handle you originally got). – Brendan May 26 '21 at 09:40
  • How to do unlink? – daniel May 27 '21 at 05:36

0 Answers0