How to make a CString to 2 lines?

D

Donos

I have a CString with 100 characters. Now i want to make that to 2
lines.

For example,

CString str = "This is just a test to find out how to break a
cstring";

I want this CString in the following format,

CString str1 = This is just a test to find
out how to break a cstring

ie, it should come in 2 lines.

I tried using line break (\n). But that doesn't work.

This was the code that i used.,

CString str = "This is just a test to find out how to break a
cstring";

int i = str.GetLength();

CString str1 = str.Left(i/2);

str1 += '\n';

str1+= str.Right(i/2);

str = str1; // But this is not working. It's not coming in 2 lines.

Is there any other way i can do this?
 
E

Erik Wikström

I have a CString with 100 characters. Now i want to make that to 2
lines.

CString is not part of the C++ standard library and thus off-topic in
this group, try asking in a group for Windows programming or MFC. Line-
breaks on Windows is usually represented by two characters, first a
carriage return and then a linefeed (\r\n), you could try using that.
 
D

Daniel T.

Erik Wikström said:
On 2007-12-09 20:32, Donos wrote:

CString is not part of the C++ standard library and thus off-topic in
this group, try asking in a group for Windows programming or MFC. Line-
breaks on Windows is usually represented by two characters, first a
carriage return and then a linefeed (\r\n), you could try using that.

As I understand it, the \n "character" is defined as whatever it takes
to break a string into two lines.

Run time libraries may convert between the newline character and some
other character(s) (or lack of characters) during execution (such as
compacting the carriage return/line feed combination into the newline
character or generating the newline character at the end of a logical
record or transforming between various record separators and the
newline character).
(http://www.osdata.com/topic/language/cplus.htm)
 
D

Daniel T.

Donos said:
I have a CString with 100 characters. Now i want to make that to 2
lines.

For example,

CString str = "This is just a test to find out how to break a
cstring";

I want this CString in the following format,

CString str1 = This is just a test to find
out how to break a cstring

ie, it should come in 2 lines.

I tried using line break (\n). But that doesn't work.

This was the code that i used.,

CString str = "This is just a test to find out how to break a
cstring";

int i = str.GetLength();

CString str1 = str.Left(i/2);

str1 += '\n';

str1+= str.Right(i/2);

str = str1; // But this is not working. It's not coming in 2 lines.

Is there any other way i can do this?

What do you mean by "coming in 2 lines"? What does the following print
on the screen?

#include <string>

int main() {
std::string str = "This is just a test to find\nout how to break a
cstring.";
cout << str;
}
 
S

Sarath

I have a CString with 100 characters. Now i want to make that to 2
lines.

For example,

CString str = "This is just a test to find out how to break a
cstring";

I want this CString in the following format,

CString str1 = This is just a test to find
out how to break a cstring

ie, it should come in 2 lines.

I tried using line break (\n). But that doesn't work.

This was the code that i used.,

CString str = "This is just a test to find out how to break a
cstring";

int i = str.GetLength();

CString str1 = str.Left(i/2);

str1 += '\n';

str1+= str.Right(i/2);

str = str1; // But this is not working. It's not coming in 2 lines.

Is there any other way i can do this?

This an offtopic to this group.
Probably you may be needed to give "\r\n" (carriage return and new
line) to make in two lines. You can use the Insert or another useful
interface to insert the desired characters inbetween the string. So
that you can avoid creating temporary objects.

Regards,
Sarath
 
R

Rolf Magnus

Daniel said:
As I understand it, the \n "character" is defined as whatever it takes
to break a string into two lines.

Not exactly. It's defined as one signle character. The transformation
happens when doing file I/O in text mode.
 
J

James Kanze

On 2007-12-09 20:32, Donos wrote:
CString is not part of the C++ standard library and thus
off-topic in this group, try asking in a group for Windows
programming or MFC. Line- breaks on Windows is usually
represented by two characters, first a carriage return and
then a linefeed (\r\n), you could try using that.

It wouldn't be C++ if that were the case. In C++, a line is a
sequence of printable characters, terminated by a '\n'
character.

Any other representation of a new line is purely external to the
program, and is mapped in std::filebuf, if the file is opened in
text mode.
 
T

Tadeusz B. Kopec

It wouldn't be C++ if that were the case. In C++, a line is a sequence
of printable characters, terminated by a '\n' character.

Any other representation of a new line is purely external to the
program, and is mapped in std::filebuf, if the file is opened in text
mode.

It's true if he tries to output his CString to a file, but maybe he tries
to show it on some label or text box.
 
P

Pete Becker

It's true if he tries to output his CString to a file, but maybe he tries
to show it on some label or text box.

Indeed. But standard C++ has neither a label (as the term is used here)
nor a text box. If some operating system has system calls that don't
handle C/C++ newlines correctly, the right place to discuss how to use
those system calls is a newsgroup that discusses that operating system.
 
J

Jim Langston

Pete said:
Indeed. But standard C++ has neither a label (as the term is used
here) nor a text box. If some operating system has system calls that
don't handle C/C++ newlines correctly, the right place to discuss how
to use those system calls is a newsgroup that discusses that
operating system.

Or just ask the OP how he knows it's not two lines, what he's using to view
them. So OP, how do you know it's not two lines? Have you tried outputing
them to std::cout?
 
J

James Kanze

Indeed. But standard C++ has neither a label (as the term is
used here) nor a text box. If some operating system has system
calls that don't handle C/C++ newlines correctly, the right
place to discuss how to use those system calls is a newsgroup
that discusses that operating system.

I'm not even sure what "handle C/C++ newlines correctly" would
mean in this context. I don't necessarily expect labels to do
formatting of any sort; alternatively, it may do HTML
formatting, and require "<br>" for a newline (treating "\n" or
"\r\n" as simply so much white space). There's no "correctly"
involved, unless the library doesn't do what its documentation
says it does (or the library doesn't document what it does).

Of course, if the original poster is using such a library, and
hasn't bothered to read its documentation, there's not much we
can do about that.
 

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,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top