开发者

Tips for embedding a longer text in source code, as in languages like Perl? [duplicate]

开发者 https://www.devze.com 2022-12-14 16:53 出处:网络
This question already has answers here: Closed 12 years ago. Possible Duplicate: Something like print END << END; in C++?
This question already has answers here: Closed 12 years ago.

Possible Duplicate:

Something like print END << END; in C++?

In a shell script or in a perl program the so called "HERE" documents are commonly used for longer text, e.g. Perl:

my $t=<<'...';

usage:

   program [options] arg1 arg2

      options:

            -opt1  description for opt1
            -opt2  description for opt2
...

print $t;

This style is very well readable, e.g. no need to escape quotes or to explicitly insert \n.

I am wondering if there is a comparable elegant approach to embed a longer text inside a C/C++ program?

#include <iostream>;
int main(void) {
  s开发者_运维百科td::string t;
  // t = ... the same long text as in the perl example in a HERE document fashion ...
  std::cout << t;
  return 0;    
}

EDIT: Simplification: there is no variable interpolation needed.


Unfortunately there's no elegant solution. I keep using:

std::string lorem =
"Lorem ipsum dolor sit amet, consectetur adipisicing elit, "
"sed do eiusmod tempor incididunt ut labore et dolore magna "
"aliqua. Ut enim ad minim veniam, quis nostrud exercitation "
"...";

C/C++ sticks the strings together, unfortunately there's no way to enter a linebreak implicitly except using \n.

Besides, this is a duplicate of this SO question.


I have always used

"....\n"
".....\n"
"....\n"

Which is actually one char* literal, and has the little advantage of not getting distorted when MSVC optimizes tabs.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号