C++ 如何打印 '\n' 而不是换行符?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1079748/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to print '\n' instead of a newline?
提问by samoz
I am writing a program that uses prints a hex dump of its input. However, I'm running into problems when newlines, tabs, etc are passed in and destroying my output formatting.
我正在编写一个程序,该程序使用打印其输入的十六进制转储。但是,当传入换行符、制表符等并破坏我的输出格式时,我遇到了问题。
How can I use printf (or cout I guess) to print '\n' instead of printing an actual newline? Do I just need to do some manual parsing for this?
如何使用 printf(或 cout 我猜)打印 '\n' 而不是打印实际的换行符?我是否只需要为此进行一些手动解析?
EDIT: I'm receiving my data dynamically, it's not just the \n that I'm corned about, but rather all symbols. For example, this is my printf statement:
编辑:我正在动态接收我的数据,这不仅仅是我所关注的 \n,而是所有符号。例如,这是我的 printf 语句:
printf("%c", theChar);
How can I make this print \n when a newline is passed in as theChar but still make it print normal text when theChar is a valid printable character?
当换行符作为 theChar 传入时,如何打印 \n 但当 theChar 是有效的可打印字符时仍使其打印正常文本?
回答by sharptooth
回答by Jared Oberhaus
The function printchar()
below will print some characters as "special", and print the octal code for characters out of range (a la Emacs), but print normal characters otherwise. I also took the liberty of having '\n'
print a real '\n'
after it to make your output more readable. Also note that I use an int
in the loop in main
just to be able to iterate over the whole range of unsigned char
. In your usage you would likely just have an unsigned char
that you read from your dataset.
printchar()
下面的函数会将一些字符打印为“特殊”,并为超出范围的字符打印八进制代码(a la Emacs),否则打印正常字符。我还冒昧'\n'
地'\n'
在它之后打印了一个真实的,以使您的输出更具可读性。另请注意,我int
在循环中使用了inmain
只是为了能够在unsigned char
. 在您的使用中,您可能只会unsigned char
从数据集中读取一个。
#include <stdio.h>
static void printchar(unsigned char theChar) {
switch (theChar) {
case '\n':
printf("\n\n");
break;
case '\r':
printf("\r");
break;
case '\t':
printf("\t");
break;
default:
if ((theChar < 0x20) || (theChar > 0x7f)) {
printf("\%03o", (unsigned char)theChar);
} else {
printf("%c", theChar);
}
break;
}
}
int main(int argc, char** argv) {
int theChar;
(void)argc;
(void)argv;
for (theChar = 0x00; theChar <= 0xff; theChar++) {
printchar((unsigned char)theChar);
}
printf("\n");
}
回答by Samuel Carrijo
Just use "\\n" (two slashes)
只需使用“\\n”(两个斜线)
回答by Jon-Eric
You can escape the backslash to make it print just a normal backslash: "\\n".
您可以转义反斜杠以使其仅打印普通的反斜杠: "\\n".
Edit: Yes you'll have to do some manual parsing. However the code to do so, would just be a search and replace.
编辑:是的,您必须进行一些手动解析。然而,这样做的代码只是一个搜索和替换。
回答by Peter Kovacs
If you want to make sure that you don't print any non-printable characters, then you can use the functions in ctype.h
like isprint
:
如果你想确保你没有打印任何非打印字符,那么您可以在使用的功能ctype.h
类似isprint
:
if( isprint( theChar ) )
printf( "%c", theChar )
else
switch( theChar )
{
case '\n':
printf( "\n" );
break;
... repeat for other interesting control characters ...
default:
printf( "\0%hho", theChar ); // print octal representation of character.
break;
}
回答by Nik
printf("\n");
回答by TheUndeadFish
In addition to the examples provided by other people, you should look at the character classification functions like isprint()and iscntrl(). Those can be used to detect which characters are or aren't printable without having to hardcode hex values from an ascii table.
除了其他人提供的示例之外,您还应该查看诸如isprint()和iscntrl() 之类的字符分类函数。这些可用于检测哪些字符可打印或不可打印,而无需从 ascii 表中硬编码十六进制值。
回答by Mr. Will
In C/C++, the '\' character is reserved as the escape character. So whenever you want to actually print a '\', you must enter '\'. So to print the actual '\n' value you would print the following:
在 C/C++ 中,'\' 字符被保留为转义字符。所以当你想真正打印一个'\'时,你必须输入'\'。因此,要打印实际的 '\n' 值,您将打印以下内容:
printf("\n");
回答by Jon Cage
Just use String::replaceto replace the offending characters before you call printf.
在调用 printf 之前,只需使用String::replace替换有问题的字符。
You could wrap the printf to do something like this:
你可以包装 printf 来做这样的事情:
void printfNeat(char* str)
{
string tidyString(str);
tidyString.replace("\n", "\n");
printf(tidyString);
}
...and just add extra replace statements to rid yourself of other unwanted characters.
...只需添加额外的替换语句即可摆脱其他不需要的字符。
[Edit]or if you want to use arguments, try this:
[编辑]或者如果你想使用参数,试试这个:
void printfNeat(char* str, ...)
{
va_list argList;
va_start(argList, msg);
string tidyString(str);
tidyString.replace("\n", "\n");
vprintf(tidyString, argList);
va_end(argList);
}
回答by Ryan Haining
As of C++11 you can also use raw strings
从 C++11 开始,您还可以使用原始字符串
std::printf(R"(\n)");
everything inside the R"(
and )"
will be printed literally. escape sequences will not be processed.
R"(
和里面的所有内容都)"
将按字面意思打印。不会处理转义序列。