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' <repeats 17 times> 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);

Recommended Answers

All 5 Replies

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?


.

> I am seeing 2:00 pm listed as "2:00 pm", '\0' <repeats 17 times>

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;

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

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.

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 copies length 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;

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.