0

I'm printing a sample string using kernel_write function and getting the output but I'm not sure about the format that it's printing in the file

Here is my code:

    char* res = "xyz";
    
    char* buffer = kmalloc(PAGE_SIZE, GFP_KERNEL);

    loff_t pos = 0;
    
    size_t resw;

    char* file i_fp = filp_open("input.txt",O_WRONLY | O_CREAT | O_APPEND, 0777);

    if (!IS_ERR(i_fp)) {
        resw = kernel_write(i_fp, (void *)buffer, strlen(res), &pos);
        printk("%ld",resw);
        printk("ff");
    }

output of the file

I want a sample string to be print in the output file

I'm getting the output in the file but the for each character it's showing '\00'. Is there some issue with my code?

Any suggestion is appreciated. Thanks in advance.

  • 1
    The variable `res` is never copied into `buffer`, so when you write out `buffer`, you're writing whatever is in that buffer beforehand. In this case, there are three null characters, so those three null characters get copied into the file. – Nick ODell Nov 30 '22 at 21:27
  • @NickODell Thanks Nick it's working now. Update Kernel_write function would be resw = kernel_write(i_fp, (void *)res, strlen(res), &pos); – Vikas Mishra Nov 30 '22 at 21:40

0 Answers0