在linux驱动开发中,经常要调试驱动,保存数据以便查看是否正确,方法如下:
代码:
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <linux/init.h>
static int filecount = 0;
filecount++
char filename[50];
sprintf(filename,"/root/saveimg/filename_screen_%d",filecount);
struct file *fp;
mm_segment_t fs;
loff_t pos;
printk("open file/n");
fp =filp_open(filename,O_RDWR | O_CREAT,0644);
if (IS_ERR(fp))
{
printk("create file error/n");
return -1;
}
fs =get_fs();
set_fs(KERNEL_DS);
pos =0;
vfs_write(fp,Surface.buffer, Surface.Width*Surface.Height*4, &pos);
pos =0;
//vfs_read(fp,buf1, sizeof(buf), &pos);
//printk("read: %s/n",buf1);
filp_close(fp,NULL);
set_fs(fs);