简体   繁体   English

如何在Linux内核模式下将'\\ n'传递到文件中

[英]How to pass '\n' into file in linux kernel mode

I write into a file const char *prefix 我写入文件const char *prefix

vfs_write(filp, prefix, strlen(prefix), &os);
   pos = pos + strlen(prefix);

But it has no '\\n' at the end. 但结尾没有'\\ n'。 I tried to write 我试图写

vfs_write(filp, '\n', strlen('\n'), &os);
   pos = pos + strlen('\n');

or 要么

vfs_write(filp, 0xAD, strlen(0xAD), &os);
   pos = pos + strlen(0xAD);

or 要么

vfs_write(filp, 0xAD, 1, &os);
   pos = pos + 1;

but it doesn't work. 但这不起作用。

How should i do that? 我该怎么办?

尝试使用字符串“ \\ n”而不是字符“ \\ n”。

The newline character is not a string, it's a character . 换行符不是字符串,而是一个字符 The function wants a pointer to a buffer of bytes (characters) and by providing a character literal you tell the function to use that character as a pointer, so it will read from address '\\n' (ie address 0x0a) which do not contain a valid string. 该函数需要一个指向字节(字符)缓冲区的指针 ,并通过提供字符文字来告诉函数将该字符用作指针,因此它将从不包含以下内容的地址'\\n' (即地址0x0a)读取一个有效的字符串。

Instead do eg vfs_write(filp, "\\n", 1, &os); 而是执行例如vfs_write(filp, "\\n", 1, &os);

The string literal "\\n" is a pointer to an constant array of characters, in other words a pointer to a buffer of bytes. 字符串文字"\\n"是指向恒定字符数组的指针,换句话说,指向字节缓冲区的指针。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM