inline text, large strings

D

Daniel Heiserer

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
 
K

Karl Heinz Buchegger

Daniel said:
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.
 
P

Pete Chapman

Daniel said:
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.
 
R

Rolf Magnus

Karl said:
"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 ................"
;
 
G

Gernot Frisch

"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
 
R

Ron Natalie

Daniel Heiserer said:
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.
 
R

Ron Natalie

Karl Heinz Buchegger said:
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"
 
G

Gernot Frisch

Ron Natalie said:
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.
 
V

Victor Bazarov

Gernot said:
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
 
G

Gernot Frisch

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
 
G

Gernot Frisch

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

Dietmar Kuehl

Gernot Frisch said:
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
 
J

Jack Klein

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

Gernot Frisch

Dietmar Kuehl said:
Gernot Frisch said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,052
Latest member
KetoBeez

Latest Threads

Top