inline text, large strings

Discussion in 'C++' started by Daniel Heiserer, Aug 17, 2004.

  1. Hi,
    I want to fill a string like this:

    using namespace std;
    string text="
    now a lot of text ................
    now a lot of text ................
    now a lot of text ................
    now a lot of text ................
    now a lot of text ................
    ";

    I get the warning message using g++ (GCC) 3.2.2 on linux:
    .......warning: multi-line string literals are deprecated

    Is there a way to write large portions of text right into the
    code without getting these warnings?
    I do not want to write something like this:

    string text;
    text+="now a lot of text ................";
    text+="now a lot of text ................";
    text+="now a lot of text ................";
    text+="now a lot of text ................";
    text+="now a lot of text ................";

    -- thanks, daniel
    Daniel Heiserer, Aug 17, 2004
    #1
    1. Advertising

  2. Daniel Heiserer wrote:
    >
    > Hi,
    > I want to fill a string like this:
    >
    > using namespace std;
    > string text="
    > now a lot of text ................
    > now a lot of text ................
    > now a lot of text ................
    > now a lot of text ................
    > now a lot of text ................
    > ";
    >
    > I get the warning message using g++ (GCC) 3.2.2 on linux:
    > ......warning: multi-line string literals are deprecated
    >
    > Is there a way to write large portions of text right into the
    > code without getting these warnings?
    > I do not want to write something like this:
    >
    > string text;
    > text+="now a lot of text ................";
    > text+="now a lot of text ................";
    > text+="now a lot of text ................";
    > text+="now a lot of text ................";
    > text+="now a lot of text ................";
    >


    "now " "a " "lot " "of " "text"

    is equivalent to "now a lot of text".
    In other words: The compiler will catanate individual string
    literals into one literal on its own.

    Therefore:

    string text= "now a lot of text ................"
    "now a lot of text ................"
    "now a lot of text ................"
    "now a lot of text ................"
    "now a lot of text ................"
    ;

    does exactly what you want.

    --
    Karl Heinz Buchegger
    Karl Heinz Buchegger, Aug 17, 2004
    #2
    1. Advertising

  3. Daniel Heiserer

    Pete Chapman Guest

    Daniel Heiserer wrote:

    > Hi,
    > I want to fill a string like this:
    >
    > using namespace std;
    > string text="
    > now a lot of text ................
    > now a lot of text ................
    > now a lot of text ................
    > ";



    I'd recommend:

    using namespace std;
    string text=
    "now a lot of text ................"
    "now a lot of text ................"
    "now a lot of text ................"
    "now a lot of text ................"
    "now a lot of text ................"
    ;

    The compiler treats this as a single string (avoiding the text+=""
    operation), and if you have a decent text editor it will be simple to
    add the quotes *after* you've typed (or cut&pasted) your text into the
    source.
    Pete Chapman, Aug 17, 2004
    #3
  4. Daniel Heiserer

    Rolf Magnus Guest

    Karl Heinz Buchegger wrote:

    > "now " "a " "lot " "of " "text"
    >
    > is equivalent to "now a lot of text".
    > In other words: The compiler will catanate individual string
    > literals into one literal on its own.
    >
    > Therefore:
    >
    > string text= "now a lot of text ................"
    > "now a lot of text ................"
    > "now a lot of text ................"
    > "now a lot of text ................"
    > "now a lot of text ................"
    > ;
    >
    > does exactly what you want.


    Unless the OP wants the newlines to be present, which he would have to
    add manually in this case:

    string text= "now a lot of text ................\n"
    "now a lot of text ................\n"
    "now a lot of text ................\n"
    "now a lot of text ................\n"
    "now a lot of text ................"
    ;
    Rolf Magnus, Aug 17, 2004
    #4
  5. > "now " "a " "lot " "of " "text"
    >
    > is equivalent to "now a lot of text".
    > In other words: The compiler will catanate individual string
    > literals into one literal on its own.


    Crap! 5 years of programming things like:

    printf(" Here\n\
    comes \
    my \
    text");
    and now you tell me I can add comments and indentination with "" ""...
    Bohooo.. All my pretty source code looks like a piece of #*%& because
    I didn't know that.
    -Gernot
    Gernot Frisch, Aug 17, 2004
    #5
  6. Daniel Heiserer

    Ron Natalie Guest

    "Daniel Heiserer" <> wrote in message news:cfstnf$...
    > I get the warning message using g++ (GCC) 3.2.2 on linux:
    > ......warning: multi-line string literals are deprecated



    Two string literals immediately abutting are merged into one:

    "a" "b" is "ab"

    "now a lot of text...\n"
    "now a lot of text...\n" ...

    does what you want I believe.
    Ron Natalie, Aug 17, 2004
    #6
  7. Daniel Heiserer

    Ron Natalie Guest

    "Karl Heinz Buchegger" <> wrote in message news:...
    > Therefore:
    >
    > string text= "now a lot of text ................"
    > "now a lot of text ................"


    Note that the new line is just extraneous white space here. If you want
    new lines in the string (as his original case did) you must explicitly provide them:

    "now a lot of text....\n" "now a lot of text\n"
    Ron Natalie, Aug 17, 2004
    #7
  8. "Ron Natalie" <> schrieb im Newsbeitrag
    news:41220219$0$2451$...
    >
    > "Karl Heinz Buchegger" <> wrote in message

    news:...
    > > Therefore:
    > >
    > > string text= "now a lot of text ................"
    > > "now a lot of text ................"

    >
    > Note that the new line is just extraneous white space here. If you

    want
    > new lines in the string (as his original case did) you must

    explicitly provide them:
    >
    > "now a lot of text....\n" "now a lot of text\n"


    The original text:
    char as[]="asasd
    asdasd
    asdasd";
    had _no_ newline characters.
    Gernot Frisch, Aug 17, 2004
    #8
  9. Gernot Frisch wrote:
    > "Ron Natalie" <> schrieb im Newsbeitrag
    > news:41220219$0$2451$...
    >
    >>"Karl Heinz Buchegger" <> wrote in message

    >
    > news:...
    >
    >>>Therefore:
    >>>
    >>>string text= "now a lot of text ................"
    >>> "now a lot of text ................"

    >>
    >>Note that the new line is just extraneous white space here. If you

    >
    > want
    >
    >>new lines in the string (as his original case did) you must

    >
    > explicitly provide them:
    >
    >> "now a lot of text....\n" "now a lot of text\n"

    >
    >
    > The original text:
    > char as[]="asasd
    > asdasd
    > asdasd";
    > had _no_ newline characters.


    If that were so, wouldn't the first 'asdasd' appear on the same line as
    the 'asasd' and the second 'asdasd' on the same line as other parts of
    the literal? What are the characters that separate them? Let me put it
    this way: what do _you_ call those characters?

    V
    Victor Bazarov, Aug 17, 2004
    #9

  10. > > The original text:
    > > char as[]="asasd
    > > asdasd
    > > asdasd";
    > > had _no_ newline characters.

    >
    > If that were so, wouldn't the first 'asdasd' appear on the same line

    as
    > the 'asasd' and the second 'asdasd' on the same line as other parts

    of
    > the literal? What are the characters that separate them? Let me

    put it
    > this way: what do _you_ call those characters?


    char a[]="a\
    b";
    printf(a);

    output:
    ab

    The '\' is only to indicate the compiler, that the next line is
    extending this line (as in the #define macros)
    -GF
    Gernot Frisch, Aug 17, 2004
    #10
  11. > The '\' is only to indicate the compiler, that the next line is
    > extending this line (as in the #define macros)


    ....which the OP didn't use. My fault, sorry for not looking exaclty.
    Flame me, I'm unworthy to post here....
    Gernot Frisch, Aug 17, 2004
    #11
  12. "Gernot Frisch" <> wrote:
    > The original text:
    > char as[]="asasd
    > asdasd
    > asdasd";
    > had _no_ newline characters.


    Since multi-line string constants are not defined in standard C++, it is
    somewhat hard to tell what the standard semantics of the above string are.
    I guess that there are at least three cases how the above string is
    treated by different systems:
    - it is an error
    - it is a string with newlines
    - it is a string without newline
    --
    <mailto:> <http://www.dietmar-kuehl.de/>
    <http://www.contendix.com> - Software Development & Consulting
    Dietmar Kuehl, Aug 17, 2004
    #12
  13. Daniel Heiserer

    Jack Klein Guest

    On Tue, 17 Aug 2004 15:04:16 +0200, "Gernot Frisch" <>
    wrote in comp.lang.c++:

    > > "now " "a " "lot " "of " "text"
    > >
    > > is equivalent to "now a lot of text".
    > > In other words: The compiler will catanate individual string
    > > literals into one literal on its own.

    >
    > Crap! 5 years of programming things like:
    >
    > printf(" Here\n\
    > comes \
    > my \
    > text");
    > and now you tell me I can add comments and indentination with "" ""...
    > Bohooo.. All my pretty source code looks like a piece of #*%& because
    > I didn't know that.
    > -Gernot


    Yes, since the very first 1989 ANSI C standard.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
    Jack Klein, Aug 18, 2004
    #13
  14. "Dietmar Kuehl" <> schrieb im Newsbeitrag
    news:...
    > "Gernot Frisch" <> wrote:
    > > The original text:
    > > char as[]="asasd
    > > asdasd
    > > asdasd";
    > > had _no_ newline characters.

    >
    > Since multi-line string constants are not defined in standard C++,

    it is
    > somewhat hard to tell what the standard semantics of the above

    string are.
    > I guess that there are at least three cases how the above string is
    > treated by different systems:
    > - it is an error
    > - it is a string with newlines
    > - it is a string without newline


    Re-checked it: VC gives an error, gcc will add a newline.
    Gernot Frisch, Aug 18, 2004
    #14
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Abhi
    Replies:
    2
    Views:
    734
    E. Robert Tisdale
    Jul 3, 2003
  2. Alvin
    Replies:
    7
    Views:
    473
    E. Robert Tisdale
    May 6, 2005
  3. Ben

    Strings, Strings and Damned Strings

    Ben, Jun 22, 2006, in forum: C Programming
    Replies:
    14
    Views:
    754
    Malcolm
    Jun 24, 2006
  4. Karin Lagesen

    matching strings in a large set of strings

    Karin Lagesen, Apr 29, 2010, in forum: Python
    Replies:
    13
    Views:
    454
    Bryan
    May 3, 2010
  5. Helmut Jarausch
    Replies:
    3
    Views:
    321
    Dave Angel
    Apr 30, 2010
Loading...

Share This Page