CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Sep 2003
    Location
    England
    Posts
    129

    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!)

  2. #2
    Join Date
    Jul 2010
    Posts
    88

    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.

  3. #3
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    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.

  4. #4
    Join Date
    Sep 2003
    Location
    England
    Posts
    129

    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.

  5. #5
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: Difference between strings before and after strncpy

    Quote Originally Posted by SteveTaylor View Post
    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.

  6. #6
    Join Date
    Jul 2010
    Posts
    88

    Re: Difference between strings before and after strncpy

    Try looping from first to last character and print their integer representation.

  7. #7
    Join Date
    Aug 2008
    Posts
    902

    Re: Difference between strings before and after strncpy

    Quote Originally Posted by SteveTaylor View Post
    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?

  8. #8
    Join Date
    Sep 2003
    Location
    England
    Posts
    129

    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.

  9. #9
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: Difference between strings before and after strncpy

    Show your code

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured