Chinaunix首页 | 论坛 | 博客
  • 博客访问: 230731
  • 博文数量: 48
  • 博客积分: 3010
  • 博客等级: 中校
  • 技术积分: 548
  • 用 户 组: 普通用户
  • 注册时间: 2008-06-28 19:18
文章分类

全部博文(48)

文章存档

2010年(6)

2009年(6)

2008年(36)

我的朋友

分类: LINUX

2008-08-13 21:49:10

#define MY_FILE "/root/logfile"
unsigned char write_file(char *data,int len)
{
                mm_segment_t old_fs;
                ssize_t result;
                unsigned long flags;
                unsigned short i;
                struct file *fd1 = NULL;
                fd1 = filp_open("MY_FILE", O_RDWR|O_CREAT,S_IRUSR|S_IWUSR);
                //|O_CREAT
                                                 
                if (fd1 == NULL)
                {                                           
                        printk("失败\n");
                        return 0;
                }
                if((!fd1->f_op) || (!fd1->f_op->write))
                {
                        filp_close(fd1,NULL);
                        return 0;
                }

                sprintf(data,"%s","the messages");//写入文件的内容
                old_fs = get_fs();
                set_fs(get_ds());
                result = fd1->f_op->write(fd1,data,len,&fd1->f_pos);
                printk(" result = %d\n",result);
                set_fs(old_fs);
                return 1;
}

调用函数()
{
        char hh[128];
        int ll;
        ll = write_file((char *)hh,sizeof(hh));
        if(ll==0)
        {
                printk("失败\n");
        }
}
 
 
 
 filp_open("MY_FILE", O_RDWR|O_CREAT,S_IRUSR|S_IWUSR);
第一个参数为建立文件的路径
第二个参数是打开文件的动作。 O_RDWR 写的方式打开
O_CREAT 是没有文件就建立文件
 
S_IRUSR|S_IWUSR  如果参数2有建立文件 那个就是建立一个可读写的文件
 
阅读(1596) | 评论(0) | 转发(0) |
0

上一篇:linux死机

下一篇:VI对齐

给主人留下些什么吧!~~