ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
As I am reading strings like
550 File not readable.
200 Command status OK.
the Buffer has contents like buffer = "550 File not readable.\r\n"
but strncpy returns result with contents "5508" and then returnval =5508.
Also,
When , 200 is received,
result contains "200("
but returnval = 200, which is actually wanted.
I want to know why the strncpy is copying 4 bits instead of 3.
Also, when I do it for 2, it copies 2 only which is the way it is supposed to work.
*The values of various buffers was read through gdb.
Thanks,
but there are multiple constraints that dont allow use of strtok here. Delimiter may change here and it would be very diffcult to operate in multiline response.
didn't see that in your code. it looks like you want result to be an array of 3 characters and for it to have the 3 digit error code. in which case strtok would work great for you. but anyway... manually add a null terminator to result after strncpy before running it through atoi.
from man strncpy:
Quote:
The strncpy() function is similar, except that not more than n bytes of
src are copied. Thus, if there is no null byte among the first n bytes of
src, the result will not be null-terminated.
also, if you want to get 3 characters, your result pointer needs to allocate a character array of 4 (1 greater) so the last character will be the null terminator.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.