0

I want to write a single variable over several lines in C++. more precisely in WINAPI.

Something like: (if \ is the command that does it,)

str=" This is a sample file trying to write multiple lines. \n but it is not same as line break. \
I am defining the same string over several lines. This is different from using backslash n. \
This is not supposed to print multipline in screen or in write file or on windows display. This\
is for ease of programming.";

The problem with this is that I got "|||" whereever I had used \ in my code. I don't want that to appear. What shall I do?

user2178841
  • 849
  • 2
  • 13
  • 26

1 Answers1

6

There are several alternatives. Here are two:

  1. Put the content of the string into a file and read the file content into the string. When you find yourself using lots of long strings, this probably the “correct” way.

  2. Use the following syntax:

    str = "This is a string that is going over several lines "
          "but it does not include line breaks and if you print "
          "the string you will see that it looks like it was "
          "written normally.";
    

    – C++ allows you to write several string literals after another and concatenates them automatically at compile time. That is, "a" "b" is the same as "ab", as far as C++ is concerned.

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214