By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,434 Members | 1,878 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,434 IT Pros & Developers. It's quick & easy.

inline text, large strings

P: 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

Jul 22 '05 #1
Share this Question
Share on Google+
13 Replies


P: n/a
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
kb******@gascad.at
Jul 22 '05 #2

P: n/a
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.
Jul 22 '05 #3

P: n/a
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 ................"
;

Jul 22 '05 #4

P: n/a
> "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
Jul 22 '05 #5

P: n/a

"Daniel Heiserer" <da*************@bmw.de> wrote in message news:cf*********@usenet.bmw.de...
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.

Jul 22 '05 #6

P: n/a

"Karl Heinz Buchegger" <kb******@gascad.at> wrote in message news:41***************@gascad.at...
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"
Jul 22 '05 #7

P: n/a

"Ron Natalie" <ro*@sensor.com> schrieb im Newsbeitrag
news:41**********************@news.newshosting.com ...

"Karl Heinz Buchegger" <kb******@gascad.at> wrote in message news:41***************@gascad.at...
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.
Jul 22 '05 #8

P: n/a
Gernot Frisch wrote:
"Ron Natalie" <ro*@sensor.com> schrieb im Newsbeitrag
news:41**********************@news.newshosting.com ...
"Karl Heinz Buchegger" <kb******@gascad.at> wrote in message


news:41***************@gascad.at...
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
Jul 22 '05 #9

P: n/a

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

Jul 22 '05 #10

P: n/a
> 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....
Jul 22 '05 #11

P: n/a
"Gernot Frisch" <Me@Privacy.net> 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:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.contendix.com> - Software Development & Consulting
Jul 22 '05 #12

P: n/a
On Tue, 17 Aug 2004 15:04:16 +0200, "Gernot Frisch" <Me@Privacy.net>
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/~a...FAQ-acllc.html
Jul 22 '05 #13

P: n/a

"Dietmar Kuehl" <di***********@yahoo.com> schrieb im Newsbeitrag
news:5b**************************@posting.google.c om...
"Gernot Frisch" <Me@Privacy.net> 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.
Jul 22 '05 #14

This discussion thread is closed

Replies have been disabled for this discussion.