473,761 Members | 9,284 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

strcat - strange behavior

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
Feb 27 '06
16 5166
In article <08************ *************** *****@4ax.com>,
Lilith <li****@dcccd.e du> 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. 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.
Feb 28 '06 #11
Daniel T. wrote:
[..]
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).

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
Feb 28 '06 #12
On 27 Feb 2006 15:17:00 -0800, "Kaz Kylheku" <kk******@gmail .com>
wrote:
Lilith wrote:
strcpy (Buffer, "MAIL FROM: ");
m_From.GetWindo wText(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.
Moreover, you do know that MFC has a CString class?
Yes.
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.
void CWnd::GetWindow Text(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.
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
Feb 28 '06 #13
On Tue, 28 Feb 2006 00:16:48 +0100, "Frank Schmidt" <fs@example.com >
wrote:

<ro**********@ gmail.com> schrieb im Newsbeitrag
news:11******* *************** @i40g2000cwc.go oglegroups.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
Feb 28 '06 #14
Lilith wrote:
On Tue, 28 Feb 2006 00:16:48 +0100, "Frank Schmidt" <fs@example.com >
wrote:
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.


Just as I suspected. That's why in my other message I asked: how do you
know? Are you just inspecting via the debugger?

The Visual C++ debugger truncates strings. If you have some "char *p"
object and you want to see more of it, you can type in an expression
like "p + 20".

The IDE does have a memory inspector which you can use instead for
larger objects.

Like I said, idiot.

Feb 28 '06 #15
Lilith wrote:
On 27 Feb 2006 15:17:00 -0800, "Kaz Kylheku" <kk******@gmail .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::GetBuf fer(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::Releas eBuffer().

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

Feb 28 '06 #16
In article <1141145231.183 064.132590
@i40g2000cwc.go oglegroups.com> , kk******@gmail. com
says...

[ ... ]
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).


It works fine on my DeathStation 9000! :-)

--
Later,
Jerry.

The universe is a figment of its own imagination.
Mar 1 '06 #17

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
2459
by: Jon | last post by:
using Borland Compiler. x = "this is a string" strcat(x,x) strcat(x,x) will produce "this is a stringthis is a stringthis is a stringthis is a stringt"
44
5811
by: Nicolas | last post by:
On most implementations of the standard C library, the functions that copy characters in a previously allocated buffer, like strcpy or strcat, are returning the pointer to that buffer. On my NetBSD system, man strcpy gives the following prototype : char *strcpy(char * restrict dst, const char * restrict src); What's the point in returning a pointer we already know before the call? Thank you in advance for an explanation.
81
7340
by: Matt | last post by:
I have 2 questions: 1. strlen returns an unsigned (size_t) quantity. Why is an unsigned value more approprate than a signed value? Why is unsighned value less appropriate? 2. Would there be any advantage in having strcat and strcpy return a pointer to the "end" of the destination string rather than returning a
8
539
by: ctara_shafa | last post by:
Hi, I have a following problem: I'm creating a list and one of the fields should contain the date. Firstly I ask the user for the year, month and day and then I'd like to collect all this data in one field. To do this I tried to concatenate those 3 numbers into one using strcat. A piece of code is as follows /*function to enter the date*/ char * date (void) {
9
3431
by: cicalese | last post by:
For starters, I am a C novice. I don't fully understand how strings work. I am running on a linux box. I compile w/ gcc. I have a string called 'myabqcommand' that is being overwritten when i use strcat() on some unrelated strings. For this code: char *myabqcommand; myabqcommand = strtok(x, "-");
87
5151
by: Robert Seacord | last post by:
The SEI has published CMU/SEI-2006-TR-006 "Specifications for Managed Strings" and released a "proof-of-concept" implementation of the managed string library. The specification, source code for the library, and other resources related to managed strings are available for download from the CERT web site at: http://www.cert.org/secure-coding/managedstring.html
12
2379
by: Eric Kaplan | last post by:
why the following code will crash? int main() { char* str = "aa"; strcat (str, "hello"); cout << str << "\n"; strcat (str, "you1"); cout << str << "\n";
0
9554
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10136
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9923
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9811
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7358
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6640
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5266
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3911
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3509
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.