| | |
GetWindowText is returning with characters that are not user entered.
Thread Solved |
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?
C Syntax (Toggle Plain Text)
#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);
Last edited by BruenorBH; Apr 22nd, 2009 at 7:47 pm.
Error: Keyboard not attached. Press F1 to continue.
I do know everything, just not all at once. It's a virtual memory problem.
I do know everything, just not all at once. It's a virtual memory problem.
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?
.
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?
.
Last edited by jephthah; Apr 22nd, 2009 at 8:07 pm.
> 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:
As a better strcpy, it's used something like this:
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:
C Syntax (Toggle Plain Text)
strncpy(Hour, ConvertTime, 2); Hour[2] = 0; /* Ensure null termination */
C Syntax (Toggle Plain Text)
char dest [ DestSize ]; strncpy ( dest, src, DestSize - 1 ); dest [ DestSize - 1 ] = 0;
Last edited by nucleon; Apr 22nd, 2009 at 8:54 pm.
I realize what I did wrong.. I adjusted the strncpy to:
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..
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
C Syntax (Toggle Plain Text)
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:
syntax - strncpy(destination, source(+how many chars from left - becomes start point), length); - () - optional
Last edited by BruenorBH; Apr 22nd, 2009 at 10:08 pm.
Error: Keyboard not attached. Press F1 to continue.
I do know everything, just not all at once. It's a virtual memory problem.
I do know everything, just not all at once. It's a virtual memory problem.
•
•
•
•
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.
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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
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
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:
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; ![]() |
Other Threads in the C Forum
- Previous Thread: C Syntax Problem
- Next Thread: Recursive binary search
| Thread Tools | Search this Thread |
#include * adobe ansi array asterisks binarysearch centimeter changingto char character cm convert copyimagefile cprogramme creafecopyofanytypeoffileinc database dynamic execv feet fgets file floatingpointvalidation fork function getlogicaldrivestrin givemetehcodez global grade gtkwinlinux hacking histogram ide inches include incrementoperators infiniteloop input interest intmain() iso kernel keyboard kilometer license linked linkedlist linux list locate looping lowest matrix meter microsoft mqqueue number oddnumber opendocumentformat opensource openwebfoundation owf pattern pdf performance pointer posix power probleminc process program programming radix recursion recv recvblocked research reversing segmentationfault sequential single socket socketprograming socketprogramming standard strchr string suggestions systemcall test threads turboc unix urboc user variable voidmain() wab whythiscodecausesegmentationfault windowsapi






