|
-
December 3rd, 2010, 07:31 AM
#1
Difference between strings before and after strncpy
Hello all,
I am reading a string from a database table which has 100 characters.
It reads into the variable correctly like this:
"Steve Taylor"
and has the remaining characters blank, which is correct. It looks like this in the Watch window when I drop the list to see each a character.
However,
looking at the string in the debugger it is something like this:
"Steve Taylor @~@"
and when I perform a strlen on it it return 135 characters despite it only being in a 100 char array.
Can anyone tell me what is going on here and how I can fix it so the data I am using matches the data in the Watch window (and the database!)
-
December 3rd, 2010, 07:41 AM
#2
Re: Difference between strings before and after strncpy
There are so many ways to interpret a string in ansi/unicode and different types of pointers that I prefer to print text to a console window whenever I need to debug a string.
-
December 3rd, 2010, 07:47 AM
#3
Re: Difference between strings before and after strncpy
What does your post have to do with strncpy? Seems like the terminating NULL is missing which you have to add yourself if you are using strncpy.
-
December 3rd, 2010, 11:43 AM
#4
Re: Difference between strings before and after strncpy
Apologies, since I wrote the title of the thread and the text of the thread I realised that the strncpy was not the issue.
What I do not understand is why the text as read from the database says
"SteveTaylor"
in the database and the debug window when the string is examined character by character but has gibberish characters in it when I try to use it or when I view in the watch window as a string.
-
December 3rd, 2010, 11:52 AM
#5
Re: Difference between strings before and after strncpy
 Originally Posted by SteveTaylor
Apologies, since I wrote the title of the thread and the text of the thread I realised that the strncpy was not the issue.
What I do not understand is why the text as read from the database says
"SteveTaylor"
in the database and the debug window when the string is examined character by character but has gibberish characters in it when I try to use it or when I view in the watch window as a string.
You haven't shown any code, but it still sound like the NULL terminator is missing.
-
December 3rd, 2010, 02:08 PM
#6
Re: Difference between strings before and after strncpy
Try looping from first to last character and print their integer representation.
-
December 3rd, 2010, 06:53 PM
#7
Re: Difference between strings before and after strncpy
 Originally Posted by SteveTaylor
What I do not understand is why the text as read from the database says "SteveTaylor"
As read from the database in your program?
Or as read from the database using a separate database managing program?
-
December 4th, 2010, 06:32 PM
#8
Re: Difference between strings before and after strncpy
Yep, it was a null terminator problem!
I can correct it by using the CString ctor
CString str = new CString(data, 10);
However, I cannot have this many allocations and deallocations in the loop.
Is there a way of using the assignment operator to do the same thing? Simply having the code CString str = data does not seem to work.
-
December 4th, 2010, 06:50 PM
#9
Re: Difference between strings before and after strncpy
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|