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:

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:

GET http://en-gb.start2.mozilla.com/firefox?client=firefox-a&rls=org.[B]mozillbÛ¿¸aÛ¿:official[/B] HTTP/1.1

Or as:

GET http://en-gb.start2.mozilla.com/firefox?client=firefox-a&rls=org.[B]mozillLþ¿ÿ-GB:official[/B] HTTP/1.1

(bold = problem)


How can I fix this problem?

Thank you in advance,
Matthew

The problem is that there is no guarantee that strncpy will put a \0 at the end of the string.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.