LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-19-2003, 09:36 PM   #1
rajatgarg
Member
 
Registered: Oct 2003
Posts: 41

Rep: Reputation: 15
strcpy problem


Hi all,

I am having a peculiar problem with strcpy.

Here is what i do,

char *buffer;
char *result=NULL;

buffer = malloc (sizeof(char) * MAX_RESPONSE_LENGTH);
result = malloc (sizeof(char) * 3);

bzero(buffer, sizeof(char) * MAX_RESPONSE_LENGTH);
bzero(result, sizeof(char) * 3);

do {
ctr++;

bytes_read = read(sockfd, buffer+i, 1);
if (bytes_read > 0) {
if ((buffer[i] == '\n') && (multiline == 0)) {
strncpy(result, buffer, 3);
returnval = atoi(result);
}


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.

Kindly help.

Regards,
Rajat Garg
SU
 
Old 11-19-2003, 10:02 PM   #2
megaspaz
Senior Member
 
Registered: Nov 2002
Location: Silly Con Valley
Distribution: Red Hat 7.3, Red Hat 9.0
Posts: 2,054

Rep: Reputation: 46
maybe try looking into strtok instead of strncpy. that's normally what i use to parse words from a string.

http://www.cplusplus.com/ref/cstring/strtok.html
 
Old 11-19-2003, 10:21 PM   #3
rajatgarg
Member
 
Registered: Oct 2003
Posts: 41

Original Poster
Rep: Reputation: 15
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.

Any reason why strncpy not working properly
 
Old 11-19-2003, 10:28 PM   #4
megaspaz
Senior Member
 
Registered: Nov 2002
Location: Silly Con Valley
Distribution: Red Hat 7.3, Red Hat 9.0
Posts: 2,054

Rep: Reputation: 46
*edit: hit the wrong friggin button. - double post*

Last edited by megaspaz; 11-19-2003 at 10:29 PM.
 
Old 11-19-2003, 10:28 PM   #5
megaspaz
Senior Member
 
Registered: Nov 2002
Location: Silly Con Valley
Distribution: Red Hat 7.3, Red Hat 9.0
Posts: 2,054

Rep: Reputation: 46
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.

Last edited by megaspaz; 11-19-2003 at 10:30 PM.
 
Old 11-20-2003, 12:46 AM   #6
megaspaz
Senior Member
 
Registered: Nov 2002
Location: Silly Con Valley
Distribution: Red Hat 7.3, Red Hat 9.0
Posts: 2,054

Rep: Reputation: 46
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.

Code:
 result = malloc (sizeof(char) * 4);
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
best way to avoid buffer overflow... with strcpy and strcat os2 Programming 4 03-02-2005 02:24 PM
diff between strcpy and strncpy djgerbavore Programming 11 08-04-2004 07:01 AM
strcpy causing segfault jpbarto Programming 17 04-07-2004 09:40 PM
question with strcpy Jo_Nak Programming 1 07-02-2003 04:23 PM
strcpy to an array of strucs gyroWang Programming 4 03-22-2003 10:01 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:21 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration