Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > inline text, large strings

Reply
Thread Tools

inline text, large strings

 
 
Daniel Heiserer
Guest
Posts: n/a
 
      08-17-2004
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

 
Reply With Quote
 
 
 
 
Karl Heinz Buchegger
Guest
Posts: n/a
 
      08-17-2004
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
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
 
Pete Chapman
Guest
Posts: n/a
 
      08-17-2004
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.
 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      08-17-2004
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 ................"
;

 
Reply With Quote
 
Gernot Frisch
Guest
Posts: n/a
 
      08-17-2004
> "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


 
Reply With Quote
 
Ron Natalie
Guest
Posts: n/a
 
      08-17-2004

"Daniel Heiserer" <(E-Mail Removed)> wrote in message news:cfstnf$(E-Mail Removed)...
> 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.

 
Reply With Quote
 
Ron Natalie
Guest
Posts: n/a
 
      08-17-2004

"Karl Heinz Buchegger" <(E-Mail Removed)> wrote in message news:(E-Mail Removed)...
> 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"
 
Reply With Quote
 
Gernot Frisch
Guest
Posts: n/a
 
      08-17-2004

"Ron Natalie" <(E-Mail Removed)> schrieb im Newsbeitrag
news:41220219$0$2451$(E-Mail Removed) ...
>
> "Karl Heinz Buchegger" <(E-Mail Removed)> wrote in message

news:(E-Mail Removed)...
> > 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.


 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      08-17-2004
Gernot Frisch wrote:
> "Ron Natalie" <(E-Mail Removed)> schrieb im Newsbeitrag
> news:41220219$0$2451$(E-Mail Removed) ...
>
>>"Karl Heinz Buchegger" <(E-Mail Removed)> wrote in message

>
> news:(E-Mail Removed)...
>
>>>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
 
Reply With Quote
 
Gernot Frisch
Guest
Posts: n/a
 
      08-17-2004


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



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
matching strings in a large set of strings Karin Lagesen Python 13 05-03-2010 03:53 PM
External Hashing [was Re: matching strings in a large set of strings] Helmut Jarausch Python 3 04-30-2010 08:44 PM
Strings, Strings and Damned Strings Ben C Programming 14 06-24-2006 05:09 AM
To inline or not to inline? Alvin C++ 7 05-06-2005 03:04 PM
inline or not to inline in C++ Abhi C++ 2 07-03-2003 12:07 AM



Advertisments