Thread: Strncpy problem.

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    2

    Strncpy problem.

    Hello, I am using strncpy to copy the first line of a multi-line buffer. I have successfully found the amount of characters to the end of the first line, and I have performed a strncpy to copy the first line of the buffer to a char *.

    However, some of the characters are being copied incorrectly.

    For example, I am trying to copy:


    Code:
    GET http://en-gb.start2.mozilla.com/firefox?client=firefox-a&rls=org.mozilla:en-GB:official HTTP/1.1 
    Host: en-gb.start2.mozilla.com
    But the first line is copied as:
    Code:
    GET http://en-gb.start2.mozilla.com/firefox?client=firefox-a&rls=org.mozillbÛ¿¸aÛ¿:official HTTP/1.1
    Or as:
    Code:
    GET http://en-gb.start2.mozilla.com/firefox?client=firefox-a&rls=org.mozillLþ¿ÿ-GB:official HTTP/1.1
    (bold = problem)


    How can I fix this problem?

    Thank you in advance,
    Matthew

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    strncpy() does NOT guarantee to append a \0 to the destination.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jun 2010
    Posts
    2
    Quote Originally Posted by Salem View Post
    strncpy() does NOT guarantee to append a \0 to the destination.
    CHEERS!, I ensured that the final character is a ' \0 ' and it works..

    Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strncpy problem
    By -EquinoX- in forum C Programming
    Replies: 4
    Last Post: 10-18-2008, 09:10 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM