GetWindowText is returning with characters that are not user entered.

Thread Solved

Join Date: Apr 2008
Posts: 31
Reputation: BruenorBH is an unknown quantity at this point 
Solved Threads: 0
BruenorBH's Avatar
BruenorBH BruenorBH is offline Offline
Light Poster

GetWindowText is returning with characters that are not user entered.

 
0
  #1
Apr 22nd, 2009
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?

  1. #define GETITEM(item) GetDlgItem(hWndDlg, item)
  2.  
  3. char ConvertTime[10] = "";
  4. char Hour[3] = "", Min[3] = "", AMPM[3] = "";
  5.  
  6. GetWindowText(GETITEM(IDC_CONVERT_TIME), ConvertTime, 8);
  7. strncpy(Hour, ConvertTime, 2);
  8. strncpy(Min, ConvertTime + 3, 2);
  9. 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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 1,602
Reputation: jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of 
Solved Threads: 120
jephthah's Avatar
jephthah jephthah is offline Offline
Posting Virtuoso

Re: GetWindowText is returning with characters that are not user entered.

 
0
  #2
Apr 22nd, 2009
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?


.
Last edited by jephthah; Apr 22nd, 2009 at 8:07 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 476
Reputation: nucleon has a spectacular aura about nucleon has a spectacular aura about 
Solved Threads: 91
nucleon's Avatar
nucleon nucleon is offline Offline
Posting Pro in Training

Re: GetWindowText is returning with characters that are not user entered.

 
0
  #3
Apr 22nd, 2009
> 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:
  1. strncpy(Hour, ConvertTime, 2);
  2. Hour[2] = 0; /* Ensure null termination */
As a better strcpy, it's used something like this:
  1. char dest [ DestSize ];
  2. strncpy ( dest, src, DestSize - 1 );
  3. dest [ DestSize - 1 ] = 0;
Last edited by nucleon; Apr 22nd, 2009 at 8:54 pm.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 31
Reputation: BruenorBH is an unknown quantity at this point 
Solved Threads: 0
BruenorBH's Avatar
BruenorBH BruenorBH is offline Offline
Light Poster

Re: GetWindowText is returning with characters that are not user entered.

 
0
  #4
Apr 22nd, 2009
I realize what I did wrong.. I adjusted the strncpy to:

  1. strncpy(Hour, ConvertTime, 2);
  2. strncpy(Min, ConvertTime + 2, 2);
  3. 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
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,378
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1466
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: GetWindowText is returning with characters that are not user entered.

 
0
  #5
Apr 22nd, 2009
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/c...tring/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.
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 476
Reputation: nucleon has a spectacular aura about nucleon has a spectacular aura about 
Solved Threads: 91
nucleon's Avatar
nucleon nucleon is offline Offline
Posting Pro in Training

Re: GetWindowText is returning with characters that are not user entered.

 
0
  #6
Apr 23rd, 2009
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;
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC