954,480 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

GetWindowText is returning with characters that are not user entered.

I am using GetWindowText to take user entered data, then seperate it out into variables, then finally use the one variable I need. .The only problem is that when debugging, I am seeing 2:00 pm listed as "2:00 pm", '\0' it should just be 2:00 pm or what the user entered. How do i get rid of this extra data?

#define GETITEM(item) GetDlgItem(hWndDlg, item)

char ConvertTime[10] = "";
char Hour[3] = "", Min[3] = "", AMPM[3] = "";

GetWindowText(GETITEM(IDC_CONVERT_TIME), ConvertTime, 8);
strncpy(Hour, ConvertTime, 2);
strncpy(Min, ConvertTime + 3, 2);
strncpy(AMPM, ConvertTime + 6, 2);
BruenorBH
Light Poster
36 posts since Apr 2008
Reputation Points: 10
Solved Threads: 0
 

not sure why the trailing NULLs are a problem. they probably existed in the memory before program startup, or were initialized in some manner during execution.

the first NULL after "2:00 pm" signals the end of the string. why do you care that there are more than one? anything after the first terminating NULL is not considered to be "extra data"

or am i missing something?


.

jephthah
Posting Maven
2,587 posts since Feb 2008
Reputation Points: 2,143
Solved Threads: 179
 

> I am seeing 2:00 pm listed as "2:00 pm", '\0'

Is the size of the buffer 25? Those zeros are normal.

But in general, you can't use strncpy like that. It will not null-terminate the strings. To use it as a substring operator, you must do something like this:

strncpy(Hour, ConvertTime, 2);
Hour[2] = 0; /* Ensure null termination */

As a better strcpy, it's used something like this:

char dest [ DestSize ];
strncpy ( dest, src, DestSize - 1 );
dest [ DestSize - 1 ] = 0;
nucleon
Posting Pro in Training
478 posts since Oct 2008
Reputation Points: 163
Solved Threads: 91
 

I realize what I did wrong.. I adjusted the strncpy to:

strncpy(Hour, ConvertTime, 2);
    strncpy(Min, ConvertTime + 2, 2);
    strncpy(AMPM, ConvertTime + 5, 2);


I did not know that those were NULL characters.. What was happening was when I brought the info back together it was coming back missing info.. from 2:00 pm it became 2:0 pm. So I adjusted the strncpy to pull 1 less charachter from the right and 1 extra from the left. Thanks all..But in general, you can't use strncpy like that. It will not null-terminate the strings. To use it as a substring operator, you must do something like this:

In all of my googling when learning how to work with C commands I found that you can use strncpy like substring...

syntax - strncpy(destination, source(+how many chars from left - becomes start point), length); - () - optional

BruenorBH
Light Poster
36 posts since Apr 2008
Reputation Points: 10
Solved Threads: 0
 

char * strncpy ( char * destination, const char * source, size_t num );

No null-character is implicitly appended to the end of destination, so destination will only be null-terminated if the length of the C string in source is less than num.

http://www.cplusplus.com/reference/clibrary/cstring/strncpy/

In your example strncpy(Hour, ConvertTime, 2); The length of ConvertTime is greater than 2, so the resultant string Hour will not be NULL-terminated. Hopefully, Hour was declared as char Hour[3]; so that it can hold the NULL terminating character.

See the example that was shown in the link I posted above how to fix that problem.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

OP> In all of my googling when learning how to work with C
OP> commands I found that you can use strncpy like substring

No one's saying you can't. It's just that if you only do this
strncpy( dest, src + start, length);

and it actually copieslength characters (as it often will when being used as a substr function) it will not automaticlly put a null character at the end to properly terminate the string. You must do that yourself, like so:
dest[length] = 0;

nucleon
Posting Pro in Training
478 posts since Oct 2008
Reputation Points: 163
Solved Threads: 91
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You