c - 内核模块编程 - 如何删除用户空间文本文件中的所有数据
问题描述
我知道这样做不值得,但我必须这样做。从该文件读取后,我想从名为 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;
}
解决方案
推荐阅读
- python - 查找每个集群的平均值并在 pandas 数据框中分配最佳集群
- google-kubernetes-engine - 如何每周安排 clamdscan(GKE 上的 cron 作业)?
- python - 在页面上找不到href
- unix - 我必须更换所有的?与 X 在 unix 除了一些有效?
- jquery - RadListBox 客户端选择活动项
- python - 为 python 服务器和烧瓶应用程序设置服务器日志记录
- mysql - 从 React 访问 Plesk DB
- spring-security - Grails spring security无法显示登录,它加载空HTML
- javascript - 未检测到的类型错误:其他为空,这个错误到底是什么?它是如何解决的?
- html - Selenium 没有在课堂上提供什么?