C++ printf: newline (\n) from commandline argument

前端 未结 7 1751
刺人心
刺人心 2020-12-20 02:07

How print format string passed as argument ?

example.cpp:

#include  
int main(int ac, char* av[]) 
{
     printf(av[1],\"anything\");         


        
7条回答
  •  隐瞒了意图╮
    2020-12-20 03:04

    It is only the compiler that converts \n etc to the actual ASCII character when it finds that sequence in a string.

    If you want to do it for a string that you get from somewhere, you need to manipulate the string directly and replace the string "\n" with a CR/LF etc. etc.

    If you do that, don't forget that "\\" becomes '\' too.

    Please never ever use char* buffers in C++, there is a nice std::string class that's safer and more elegant.

提交回复
热议问题