C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

strcat - strange behavior
Goto page 1, 2  Next
 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Lilith
Guest





PostPosted: Mon Feb 27, 2006 10:06 pm    Post subject: strcat - strange behavior Reply with quote



I'm working on a simple piece of code under VC++ 6.0. I've got a char
Buffer array into which I copy the contents of an MFC control. The
string is properly nul terminated. I use strcat (Buffer, "\r\n") to
append a carriage return\line feed to the string before outputting it
to a display where I can monitor what's being exchanged with another
service. But after the strcat the string in Buffer is terminated, not
with \r\n but with a \n and NO nul terminator. The carriage return is
dropped completely.

If this was output for a text file I'd think it was an expected
conversion. But all of this is still in memory.

TIA,
Lilith
Back to top
Lilith
Guest





PostPosted: Mon Feb 27, 2006 11:06 pm    Post subject: Re: strcat - strange behavior Reply with quote



On Mon, 27 Feb 2006 17:08:53 -0500, Victor Bazarov
<v.Abazarov (AT) comAcast (DOT) net> wrote:

Quote:
Lilith wrote:
I'm working on a simple piece of code under VC++ 6.0. I've got a char
Buffer array into which I copy the contents of an MFC control.

You know that MFC is off-topic, right? Just checking. I understand that
you're not asking about MFC per se.

Even when I'm striving for completeness?

Quote:
The
string is properly nul terminated. I use strcat (Buffer, "\r\n") to
append a carriage return\line feed to the string before outputting it
to a display

Outputting a string to a display is not defined in C++, you know that,
right? There is no such thing as "display" in C++.

Yes, and once again, the purpose was completeness. I wanted it
understood why I was appending the CR\LF combination.

Quote:
where I can monitor what's being exchanged with another
service. But after the strcat the string in Buffer is terminated, not
with \r\n but with a \n and NO nul terminator.

I think this is covered in the FAQ. See 5.8.

The carriage return is
dropped completely.

If this was output for a text file I'd think it was an expected
conversion. But all of this is still in memory.

I'll believe you when I see the code.

strcpy (Buffer, "MAIL FROM: ");
m_From.GetWindowText(Text, 512); // sorry if this is MFC
strcat (Buffer, Text); // up to this point the string is nul
// terminated

// Buffer is passed to a function but is still null
// terminated following the call

strcat(Buffer, "\r\n"); // after this operation the
// string is LF terminated with no
// nul

Quote:
V

--
Lilith
Back to top
Guest






PostPosted: Mon Feb 27, 2006 11:06 pm    Post subject: Re: strcat - strange behavior Reply with quote



Lilith wrote:
Quote:
On Mon, 27 Feb 2006 17:08:53 -0500, Victor Bazarov
v.Abazarov (AT) comAcast (DOT) net> wrote:

Lilith wrote:
I'm working on a simple piece of code under VC++ 6.0. I've got a char
Buffer array into which I copy the contents of an MFC control.

You know that MFC is off-topic, right? Just checking. I understand that
you're not asking about MFC per se.

Even when I'm striving for completeness?

The
string is properly nul terminated. I use strcat (Buffer, "\r\n") to
append a carriage return\line feed to the string before outputting it
to a display

Outputting a string to a display is not defined in C++, you know that,
right? There is no such thing as "display" in C++.

Yes, and once again, the purpose was completeness. I wanted it
understood why I was appending the CR\LF combination.

where I can monitor what's being exchanged with another
service. But after the strcat the string in Buffer is terminated, not
with \r\n but with a \n and NO nul terminator.

I think this is covered in the FAQ. See 5.8.

The carriage return is
dropped completely.

If this was output for a text file I'd think it was an expected
conversion. But all of this is still in memory.

I'll believe you when I see the code.

strcpy (Buffer, "MAIL FROM: ");
m_From.GetWindowText(Text, 512); // sorry if this is MFC
strcat (Buffer, Text); // up to this point the string is nul
// terminated

// Buffer is passed to a function but is still null
// terminated following the call

strcat(Buffer, "\r\n"); // after this operation the
// string is LF terminated with no
// nul

What is strlen(Buffer) vs. sizeof(Buffer)?
Back to top
Lilith
Guest





PostPosted: Mon Feb 27, 2006 11:06 pm    Post subject: Re: strcat - strange behavior Reply with quote

On 27 Feb 2006 14:43:18 -0800, roberts.noah (AT) gmail (DOT) com wrote:

Quote:

Lilith wrote:
On Mon, 27 Feb 2006 17:08:53 -0500, Victor Bazarov
v.Abazarov (AT) comAcast (DOT) net> wrote:

Lilith wrote:
I'm working on a simple piece of code under VC++ 6.0. I've got a char
Buffer array into which I copy the contents of an MFC control.

You know that MFC is off-topic, right? Just checking. I understand that
you're not asking about MFC per se.

Even when I'm striving for completeness?

The
string is properly nul terminated. I use strcat (Buffer, "\r\n") to
append a carriage return\line feed to the string before outputting it
to a display

Outputting a string to a display is not defined in C++, you know that,
right? There is no such thing as "display" in C++.

Yes, and once again, the purpose was completeness. I wanted it
understood why I was appending the CR\LF combination.

where I can monitor what's being exchanged with another
service. But after the strcat the string in Buffer is terminated, not
with \r\n but with a \n and NO nul terminator.

I think this is covered in the FAQ. See 5.8.

The carriage return is
dropped completely.

If this was output for a text file I'd think it was an expected
conversion. But all of this is still in memory.

I'll believe you when I see the code.

strcpy (Buffer, "MAIL FROM: ");
m_From.GetWindowText(Text, 512); // sorry if this is MFC
strcat (Buffer, Text); // up to this point the string is nul
// terminated

// Buffer is passed to a function but is still null
// terminated following the call

strcat(Buffer, "\r\n"); // after this operation the
// string is LF terminated with no
// nul

What is strlen(Buffer) vs. sizeof(Buffer)?

sizeof(Buffer) = 512
strlen(Buffer) := 50

Lots of room.

--
Lilith
Back to top
Victor Bazarov
Guest





PostPosted: Mon Feb 27, 2006 11:06 pm    Post subject: Re: strcat - strange behavior Reply with quote

Lilith wrote:
Quote:
I'm working on a simple piece of code under VC++ 6.0. I've got a char
Buffer array into which I copy the contents of an MFC control.

You know that MFC is off-topic, right? Just checking. I understand that
you're not asking about MFC per se.

Quote:
The
string is properly nul terminated. I use strcat (Buffer, "\r\n") to
append a carriage return\line feed to the string before outputting it
to a display

Outputting a string to a display is not defined in C++, you know that,
right? There is no such thing as "display" in C++.

Quote:
where I can monitor what's being exchanged with another
service. But after the strcat the string in Buffer is terminated, not
with \r\n but with a \n and NO nul terminator.

I think this is covered in the FAQ. See 5.8.

Quote:
The carriage return is
dropped completely.

If this was output for a text file I'd think it was an expected
conversion. But all of this is still in memory.

I'll believe you when I see the code.

V
--
Please remove capital As from my address when replying by mail
Back to top
Guest






PostPosted: Tue Feb 28, 2006 12:06 am    Post subject: Re: strcat - strange behavior Reply with quote

Lilith wrote:

Quote:
What is strlen(Buffer) vs. sizeof(Buffer)?

sizeof(Buffer) = 512
strlen(Buffer) := 50

Lots of room.

Skirt the issue then, set buffer to all 0 before processing. Then if
it fails something is modifying it in a way you are not expecting and
you can look for it.
Back to top
Frank Schmidt
Guest





PostPosted: Tue Feb 28, 2006 12:06 am    Post subject: Re: strcat - strange behavior Reply with quote

<roberts.noah (AT) gmail (DOT) com> schrieb im Newsbeitrag
news:1141081683.584734.323970 (AT) i40g2000cwc (DOT) googlegroups.com...
Quote:

Lilith wrote:

What is strlen(Buffer) vs. sizeof(Buffer)?

sizeof(Buffer) = 512
strlen(Buffer) := 50

Lots of room.

Skirt the issue then, set buffer to all 0 before processing. Then if
it fails something is modifying it in a way you are not expecting and
you can look for it.


does a strlen of 50 not kinda indicates the real length and that there is a
0 and a wrong "DISPLAY"?
Back to top
Kaz Kylheku
Guest





PostPosted: Tue Feb 28, 2006 12:06 am    Post subject: Re: strcat - strange behavior Reply with quote

Lilith wrote:
Quote:
strcpy (Buffer, "MAIL FROM: ");
m_From.GetWindowText(Text, 512); // sorry if this is MFC
strcat (Buffer, Text); // up to this point the string is nul
// terminated

Neither strcpy() nor strcat() have any mechanism to guard against
storing more characters than the destination array can hold.

Quote:
// Buffer is passed to a function but is still null
// terminated following the call

strcat(Buffer, "\r\n"); // after this operation the
// string is LF terminated with no
// nul

How do you know? Are you by chance relying on the variable inspection
window of your debugger? That Microsoft thing has certain issues when
it comes to displaying strings. It cuts off strings that are too long,
and I think it basically just stuffs the text into its own control, so
that \r\n just becomes a line break. I haven't used it in a long time,
so I can't quite remember. If you want to be sure what's in that
character array, you should convert it yourself to printable form and
dump it to a debug trace function.

Oh, you do know that C++ has a string class, right?

Moreover, you do know that MFC has a CString class?

Moreover, you do know that the control you are using has a
GetWindowText() overload that will put data into a CString object?

void CWnd::GetWindowText(CString& rString) const;

Why are you messing around with strcat instead of using the string type
provided by the framework in which you are making the GUI?

Idiot!
Back to top
Victor Bazarov
Guest





PostPosted: Tue Feb 28, 2006 12:06 am    Post subject: Re: strcat - strange behavior Reply with quote

Kaz Kylheku wrote:
Quote:
Victor Bazarov wrote:

Outputting a string to a display is not defined in C++, you know that,
right? There is no such thing as "display" in C++.


There doesn't have to be, because that's covered in the C standard.

Section "5.2.2 Character display semantics" in 9899:1999.

:)

How is that relevant? AFAIK, C++ Standard only refers to 9899:1990. Smile
Back to top
Kaz Kylheku
Guest





PostPosted: Tue Feb 28, 2006 12:06 am    Post subject: Re: strcat - strange behavior Reply with quote

Victor Bazarov wrote:
Quote:
Outputting a string to a display is not defined in C++, you know that,
right? There is no such thing as "display" in C++.

There doesn't have to be, because that's covered in the C standard.

Section "5.2.2 Character display semantics" in 9899:1999.

:)
Back to top
Daniel T.
Guest





PostPosted: Tue Feb 28, 2006 2:06 am    Post subject: Re: strcat - strange behavior Reply with quote

In article <08t6025tuhupv5c0e1i2e98aggv9vluho5 (AT) 4ax (DOT) com>,
Lilith <lilith (AT) dcccd (DOT) edu> wrote:

Quote:
I'm working on a simple piece of code under VC++ 6.0. I've got a char
Buffer array into which I copy the contents of an MFC control. The
string is properly nul terminated. I use strcat (Buffer, "\r\n") to
append a carriage return\line feed to the string before outputting it
to a display where I can monitor what's being exchanged with another
service. But after the strcat the string in Buffer is terminated, not
with \r\n but with a \n and NO nul terminator. The carriage return is
dropped completely.

If this was output for a text file I'd think it was an expected
conversion. But all of this is still in memory.

TIA,
Lilith

Lilith, try this:

int main()
{
char buffer[10] = "hello";
strcat( buffer, "\r\n" );
assert( buffer[5] == '\r' );
assert( buffer[6] == '\n' );
assert( buffer[7] == 0 );
cout << "working";
}

If "working" doesn't print out, then I'd say your strcat function is
broken.

--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.
Back to top
Victor Bazarov
Guest





PostPosted: Tue Feb 28, 2006 5:06 am    Post subject: Re: strcat - strange behavior Reply with quote

Daniel T. wrote:
Quote:
[..]
Lilith, try this:

... after adding this:

#include <iostream>
#include <cstring>
#inlcude <cassert>
using namespace std;

(it can be enough to add <iostream> only, but to be complete, use
all headers necessary).

Quote:

int main()
{
char buffer[10] = "hello";
strcat( buffer, "\r\n" );
assert( buffer[5] == '\r' );
assert( buffer[6] == '\n' );
assert( buffer[7] == 0 );
cout << "working";
}

If "working" doesn't print out, then I'd say your strcat function is
broken.


V
--
Please remove capital As from my address when replying by mail
Back to top
Lilith
Guest





PostPosted: Tue Feb 28, 2006 6:06 am    Post subject: Re: strcat - strange behavior Reply with quote

On 27 Feb 2006 15:17:00 -0800, "Kaz Kylheku" <kkylheku (AT) gmail (DOT) com>
wrote:

Quote:
Lilith wrote:
strcpy (Buffer, "MAIL FROM: ");
m_From.GetWindowText(Text, 512); // sorry if this is MFC
strcat (Buffer, Text); // up to this point the string is nul
// terminated

Neither strcpy() nor strcat() have any mechanism to guard against
storing more characters than the destination array can hold.

// Buffer is passed to a function but is still null
// terminated following the call

strcat(Buffer, "\r\n"); // after this operation the
// string is LF terminated with no
// nul

How do you know? Are you by chance relying on the variable inspection
window of your debugger? That Microsoft thing has certain issues when
it comes to displaying strings. It cuts off strings that are too long,
and I think it basically just stuffs the text into its own control, so
that \r\n just becomes a line break. I haven't used it in a long time,
so I can't quite remember. If you want to be sure what's in that
character array, you should convert it yourself to printable form and
dump it to a debug trace function.

Oh, you do know that C++ has a string class, right?

Yes.

Quote:
Moreover, you do know that MFC has a CString class?

Yes.

Quote:
Moreover, you do know that the control you are using has a
GetWindowText() overload that will put data into a CString object?

Yes. And under other circumstances I've used it extensively for
getting and setting the text of controls.

Quote:
void CWnd::GetWindowText(CString& rString) const;

Why are you messing around with strcat instead of using the string type
provided by the framework in which you are making the GUI?

Because the function to which I pass the string specifies a char
array. And, yes, I know that CString resolves to a character pointer
to the character array. It's not my function but I know that the
function ends up outputting the string with more than what I feed it.
So I don't trust it not to manipulate the string before using it. So
I'm playing it safe.

Quote:
Idiot!

And that's why I don't learn a damn thing. Too many people willing to
exercise their expertise muscles for slamming people rather than
actually trying to help. It's why I hesitate to ask anything in these
forums. It's why I put more detail than some people seem to think I
should so I don't get someone who calls me an "idiot" for not being
explicit enough. Instead I get chidded for mentioning M*C in my post.

Thanks to everyone who tried to help. I'm out of here.

Idiot!

--
Lilith
Back to top
Lilith
Guest





PostPosted: Tue Feb 28, 2006 6:06 am    Post subject: Re: strcat - strange behavior Reply with quote

On Tue, 28 Feb 2006 00:16:48 +0100, "Frank Schmidt" <fs (AT) example (DOT) com>
wrote:

Quote:

roberts.noah (AT) gmail (DOT) com> schrieb im Newsbeitrag
news:1141081683.584734.323970 (AT) i40g2000cwc (DOT) googlegroups.com...

Lilith wrote:

What is strlen(Buffer) vs. sizeof(Buffer)?

sizeof(Buffer) = 512
strlen(Buffer) := 50

Lots of room.

Skirt the issue then, set buffer to all 0 before processing. Then if
it fails something is modifying it in a way you are not expecting and
you can look for it.


does a strlen of 50 not kinda indicates the real length and that there is a
0 and a wrong "DISPLAY"?

No. All my inspection is done in debug mode. I'm looking at the
string's individual chars and the indexes for the array.

--
Lilith
Back to top
Kaz Kylheku
Guest





PostPosted: Tue Feb 28, 2006 5:06 pm    Post subject: Re: strcat - strange behavior Reply with quote

Lilith wrote:
Quote:
On 27 Feb 2006 15:17:00 -0800, "Kaz Kylheku" <kkylheku (AT) gmail (DOT) com
wrote:
Why are you messing around with strcat instead of using the string type
provided by the framework in which you are making the GUI?

Because the function to which I pass the string specifies a char
array. And, yes, I know that CString resolves to a character pointer
to the character array. It's not my function but I know that the
function ends up outputting the string with more than what I feed it.
So I don't trust it not to manipulate the string before using it. So
I'm playing it safe.

Microsoft's CString class supports direct manipulation of the string
data.

It a function by means of which you can pull out a modifiable buffer.
It's different from the char * operator that pulls out a const string
implicitly:

CString::GetBuffer(int);

The parameter lets you specify how big the buffer should be. It seems
to be designed for the kind of hack where someone wants to add a bit of
data at the end. When you are done fiddling with the buffer, you call

CString::ReleaseBuffer().

if you need to do more things with the CString data using its own
methods before you destroy it.

RTFMSDN.

In your case, you can pass in a parameter of 512.

(That's Big Enough (TM) for any function that is suspected of adding
data beyond the end of the string, right? Haha).
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.