Open my Blog,Open my world!

      To be a real and excellent  ITer,    I should , from now on ,or rather off the bat, start my way to accomplish the goal.

       Blog, a new form  expressing our views,feelings to the public or only our friends around us, perhaps a good way to assist me with reaching my dreams.

       So, I must be determined to be a persistent man, then with great passion to open a new road of my future.

       In order to realize my dreams, I have to conquer myself, which I mean, struggle with my shortcomings,  and finally, kick them out of me,Although it seems to be very tough.

       And my world is now being developed, with the deveopment the World's IT.

      

在安卓内核中,可以使用file_open()函数来创建文件。以下是一个简单的示例代码: ```c #include <linux/fs.h> #include <linux/uaccess.h> static int __init my_init(void) { char *data = "Hello World!\n"; int len = strlen(data); struct file *filep; mm_segment_t old_fs; // 打开文件 filep = filp_open("/data/test.txt", O_CREAT | O_WRONLY, 0644); if (IS_ERR(filep)) { printk(KERN_ERR "Failed to create file\n"); return -1; } // 写入数据 old_fs = get_fs(); set_fs(KERNEL_DS); if (len != vfs_write(filep, data, len, &filep->f_pos)) { printk(KERN_ERR "Failed to write to file\n"); filp_close(filep, NULL); return -1; } set_fs(old_fs); // 关闭文件 filp_close(filep, NULL); return 0; } static void __exit my_exit(void) { vfs_unlink("/data/test.txt", NULL); } module_init(my_init); module_exit(my_exit); ``` 在上面的示例中,我们使用了filp_open()函数来创建文件并打开它。我们向该函数传递了文件路径、打开标志和文件权限。在这里,我们使用了O_CREAT标志,它将创建一个新文件(如果文件不存在),并使用指定的权限设置文件权限。打开标志还包括O_WRONLY,它表示我们将用于写入文件。 然后,我们使用vfs_write()函数向文件写入数据。该函数的参数是文件指针、写入的数据、数据长度和文件偏移量。我们还使用了set_fs()函数将地址空间从用户空间切换到内核空间,以便我们可以安全地访问用户空间的数据。 最后,在模块退出时,我们使用vfs_unlink()函数删除文件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值