Connecting Tech Pros Worldwide Forums | Help | Site Map

inline text, large strings

Daniel Heiserer
Guest
 
Posts: n/a
#1: Jul 22 '05
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


Karl Heinz Buchegger
Guest
 
Posts: n/a
#2: Jul 22 '05

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
Pete Chapman
Guest
 
Posts: n/a
#3: Jul 22 '05

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.
Rolf Magnus
Guest
 
Posts: n/a
#4: Jul 22 '05

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

Gernot Frisch
Guest
 
Posts: n/a
#5: Jul 22 '05

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


Ron Natalie
Guest
 
Posts: n/a
#6: Jul 22 '05

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.

Ron Natalie
Guest
 
Posts: n/a
#7: Jul 22 '05

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"
Gernot Frisch
Guest
 
Posts: n/a
#8: Jul 22 '05

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.


Victor Bazarov
Guest
 
Posts: n/a
#9: Jul 22 '05

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
Gernot Frisch
Guest
 
Posts: n/a
#10: Jul 22 '05

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



Gernot Frisch
Guest
 
Posts: n/a
#11: Jul 22 '05

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


Dietmar Kuehl
Guest
 
Posts: n/a
#12: Jul 22 '05

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
Jack Klein
Guest
 
Posts: n/a
#13: Jul 22 '05

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
Gernot Frisch
Guest
 
Posts: n/a
#14: Jul 22 '05

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