Connecting Tech Pros Worldwide Help | Site Map

inline text, large strings

  #1  
Old July 22nd, 2005, 06:59 PM
Daniel Heiserer
Guest
 
Posts: n/a
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

  #2  
Old July 22nd, 2005, 06:59 PM
Karl Heinz Buchegger
Guest
 
Posts: n/a

re: inline text, large strings


Daniel Heiserer wrote:[color=blue]
>
> 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 ................";
>[/color]

"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
kbuchegg@gascad.at
  #3  
Old July 22nd, 2005, 06:59 PM
Pete Chapman
Guest
 
Posts: n/a

re: inline text, large strings


Daniel Heiserer wrote:
[color=blue]
> 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 ................
> ";[/color]


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.
  #4  
Old July 22nd, 2005, 06:59 PM
Rolf Magnus
Guest
 
Posts: n/a

re: inline text, large strings


Karl Heinz Buchegger wrote:
[color=blue]
> "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.[/color]

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 ................"
;

  #5  
Old July 22nd, 2005, 06:59 PM
Gernot Frisch
Guest
 
Posts: n/a

re: inline text, large strings


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

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


  #6  
Old July 22nd, 2005, 06:59 PM
Ron Natalie
Guest
 
Posts: n/a

re: inline text, large strings



"Daniel Heiserer" <daniel.heiserer@bmw.de> wrote in message news:cfstnf$stg1@usenet.bmw.de...[color=blue]
> I get the warning message using g++ (GCC) 3.2.2 on linux:
> ......warning: multi-line string literals are deprecated[/color]


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.

  #7  
Old July 22nd, 2005, 06:59 PM
Ron Natalie
Guest
 
Posts: n/a

re: inline text, large strings



"Karl Heinz Buchegger" <kbuchegg@gascad.at> wrote in message news:4121FD24.45231512@gascad.at...[color=blue]
> Therefore:
>
> string text= "now a lot of text ................"
> "now a lot of text ................"[/color]

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"
  #8  
Old July 22nd, 2005, 06:59 PM
Gernot Frisch
Guest
 
Posts: n/a

re: inline text, large strings



"Ron Natalie" <ron@sensor.com> schrieb im Newsbeitrag
news:41220219$0$2451$9a6e19ea@news.newshosting.com ...[color=blue]
>
> "Karl Heinz Buchegger" <kbuchegg@gascad.at> wrote in message[/color]
news:4121FD24.45231512@gascad.at...[color=blue][color=green]
> > Therefore:
> >
> > string text= "now a lot of text ................"
> > "now a lot of text ................"[/color]
>
> Note that the new line is just extraneous white space here. If you[/color]
want[color=blue]
> new lines in the string (as his original case did) you must[/color]
explicitly provide them:[color=blue]
>
> "now a lot of text....\n" "now a lot of text\n"[/color]

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


  #9  
Old July 22nd, 2005, 06:59 PM
Victor Bazarov
Guest
 
Posts: n/a

re: inline text, large strings


Gernot Frisch wrote:[color=blue]
> "Ron Natalie" <ron@sensor.com> schrieb im Newsbeitrag
> news:41220219$0$2451$9a6e19ea@news.newshosting.com ...
>[color=green]
>>"Karl Heinz Buchegger" <kbuchegg@gascad.at> wrote in message[/color]
>
> news:4121FD24.45231512@gascad.at...
>[color=green][color=darkred]
>>>Therefore:
>>>
>>>string text= "now a lot of text ................"
>>> "now a lot of text ................"[/color]
>>
>>Note that the new line is just extraneous white space here. If you[/color]
>
> want
>[color=green]
>>new lines in the string (as his original case did) you must[/color]
>
> explicitly provide them:
>[color=green]
>> "now a lot of text....\n" "now a lot of text\n"[/color]
>
>
> The original text:
> char as[]="asasd
> asdasd
> asdasd";
> had _no_ newline characters.[/color]

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
  #10  
Old July 22nd, 2005, 06:59 PM
Gernot Frisch
Guest
 
Posts: n/a

re: inline text, large strings



[color=blue][color=green]
> > The original text:
> > char as[]="asasd
> > asdasd
> > asdasd";
> > had _no_ newline characters.[/color]
>
> If that were so, wouldn't the first 'asdasd' appear on the same line[/color]
as[color=blue]
> the 'asasd' and the second 'asdasd' on the same line as other parts[/color]
of[color=blue]
> the literal? What are the characters that separate them? Let me[/color]
put it[color=blue]
> this way: what do _you_ call those characters?[/color]

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



  #11  
Old July 22nd, 2005, 06:59 PM
Gernot Frisch
Guest
 
Posts: n/a

re: inline text, large strings


> The '\' is only to indicate the compiler, that the next line is[color=blue]
> extending this line (as in the #define macros)[/color]

....which the OP didn't use. My fault, sorry for not looking exaclty.
Flame me, I'm unworthy to post here....


  #12  
Old July 22nd, 2005, 06:59 PM
Dietmar Kuehl
Guest
 
Posts: n/a

re: inline text, large strings


"Gernot Frisch" <Me@Privacy.net> wrote:[color=blue]
> The original text:
> char as[]="asasd
> asdasd
> asdasd";
> had _no_ newline characters.[/color]

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:dietmar_kuehl@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.contendix.com> - Software Development & Consulting
  #13  
Old July 22nd, 2005, 07:00 PM
Jack Klein
Guest
 
Posts: n/a

re: inline text, large strings


On Tue, 17 Aug 2004 15:04:16 +0200, "Gernot Frisch" <Me@Privacy.net>
wrote in comp.lang.c++:
[color=blue][color=green]
> > "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.[/color]
>
> 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[/color]

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/~a...FAQ-acllc.html
  #14  
Old July 22nd, 2005, 07:00 PM
Gernot Frisch
Guest
 
Posts: n/a

re: inline text, large strings



"Dietmar Kuehl" <dietmar_kuehl@yahoo.com> schrieb im Newsbeitrag
news:5b15f8fd.0408170921.5d885b57@posting.google.c om...[color=blue]
> "Gernot Frisch" <Me@Privacy.net> wrote:[color=green]
> > The original text:
> > char as[]="asasd
> > asdasd
> > asdasd";
> > had _no_ newline characters.[/color]
>
> Since multi-line string constants are not defined in standard C++,[/color]
it is[color=blue]
> somewhat hard to tell what the standard semantics of the above[/color]
string are.[color=blue]
> 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[/color]

Re-checked it: VC gives an error, gcc will add a newline.


Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
Literals and strings - make it stop!! RobinS answers 56 April 5th, 2007 07:35 AM
C# String Comparison, IndexOf and Related BILL answers 5 November 16th, 2005 10:31 AM
how to replace a substring in a string using C? Paul answers 19 November 15th, 2005 05:39 AM
Reading a large number of text files into an array Matthew Crema answers 4 November 14th, 2005 09:46 PM