Extracting first n chars from a string

  • Follow


Ok guys,
 here is the deal, i hope someone can help me out with this.

I receive a string through a socket...i dunno how long this string is,
but i save it all into a buf.

..
..
..
[code]
if (!fork()) { // this is the child process
close(sockfd); // child doesn't need the listener
if ((numbytes=recv(new_fd, buf, MAXDATASIZE-1, 0)) == -1) {
	perror("recv");
	exit(1);
			}

buf[numbytes] = '\0';
printf("Received: %s\n",buf);
[/code]

now i know what the first 10 chars of the string in buf is, and i copy
it into another variable
..
..
..
[code]

//extract clearance and copy it into extract_clearance
strncpy(extract_clearance,buf,10);
//printf("clearnace: %s\n",extract_clearance);

[/code]


Now...how can i save the rest (what comes after the first 10 chars)
into a seocd variable?
0
Reply kifah (10) 1/20/2004 8:17:37 PM

Kifah Abbad wrote:
> 
> Ok guys,
>  here is the deal, i hope someone can help me out with this.
> 
> I receive a string through a socket...i dunno how long this string is,
> but i save it all into a buf.
> 
> .
> .
> .
> [code]
> if (!fork()) { // this is the child process
> close(sockfd); // child doesn't need the listener
> if ((numbytes=recv(new_fd, buf, MAXDATASIZE-1, 0)) == -1) {
>         perror("recv");
>         exit(1);
>                         }
> 
> buf[numbytes] = '\0';
> printf("Received: %s\n",buf);
> [/code]
> 
> now i know what the first 10 chars of the string in buf is, and i copy
> it into another variable
> .
> .
> .
> [code]
> 
> //extract clearance and copy it into extract_clearance
> strncpy(extract_clearance,buf,10);
> //printf("clearnace: %s\n",extract_clearance);
> 
> [/code]
> 
> Now...how can i save the rest (what comes after the first 10 chars)
> into a seocd variable?

    "If I have twelve bananas and give away ten of them, how
many bananas remain?"

	memcpy (wherever, buf + 10, numbytes - 10);

    A few other observations:

    -	Why are you using strncpy() instead of memcpy()
	to extract the first ten bytes?  Is there something
	about the data format you haven't told us?

    -	You should consider what to do if `numbytes' is
	less than ten.

    -	Why are you doing all this copying?  Why not just
	leave things in `buf' and use a fresh buffer for
	the next batch of incoming data?  Copying data
	unchanged from one place to another doesn't advance
	the state of the computation very much ...

    -	fork(), close(), and recv() are not Standard C
	library functions.  Also, although exit() is a
	Standard function, exit(1) uses a non-portable
	termination status.

-- 
Eric.Sosman@sun.com
0
Reply Eric.Sosman (4228) 1/20/2004 8:42:10 PM


Kifah Abbad wrote:

> strncpy(extract_clearance,buf,10);

Note that extract_clearance will not be \0-terminated
if strlen(buf) >= 10.  If you want it to be, try

  *extract_clearance = '\0';
  strncat(extract_clearance,buf,10);

> Now...how can i save the rest (what comes after the first 10 chars)
> into a seocd variable?

  var = malloc(numbytes > 10 ? numbytes - 9 : 1);
  if (var == NULL) { ERROR; }
  if (numbytes > 10)
    strcpy(var, buf + 10);
  else
    *var = '\0';

-- 
Hallvard
0
Reply Hallvard 1/20/2004 8:42:45 PM

Kifah Abbad wrote:
....
> //extract clearance and copy it into extract_clearance
> strncpy(extract_clearance,buf,10);

strncpy does not write a terminating '\0' in this case.
So you must either initialize extract_clearance with zeros

   char extract_clearance[11] = {0};

or write a terminating '\0' yourself.

   extract_clearance[10] = '\0';

> Now...how can i save the rest (what comes after the first 10 chars)
> into a seocd variable?

   char second[N] = {0};
   strncpy(second, buf + 10, sizeof second - 1);

or

   char second[N];
   strncpy(second, buf + 10, sizeof second - 1);
   second[sizeof second - 1] = 0;

Jirka
0
Reply jklaue2 (127) 1/20/2004 8:47:58 PM

Kifah Abbad wrote:
> 

> buf[numbytes] = '\0';
> printf("Received: %s\n",buf);
> [/code]
> 
> now i know what the first 10 chars of the string in buf is, and i copy
> it into another variable


> //extract clearance and copy it into extract_clearance
> strncpy(extract_clearance,buf,10);
> //printf("clearnace: %s\n",extract_clearance);

This is a problem, you didn't null-terminate this new string, strncpy()
will not do that for you unless the input string was less than the
number of chars copied. This assumes that num_received is greater than
10.

I prefer:

extract_clearance[0] = 0;

strncat (extract_clearance,buf,10);


> Now...how can i save the rest (what comes after the first 10 chars)
> into a seocd variable?


char *rest;

/*storage for the num received, - the 10 already copied, + null term*/
rest = malloc (numbytes-10+1);  

if (rest == NULL)
{
/*do error handling*/
}

else
{
   strcpy (rest, buf+10); 
}



Brian Rodenborn
0
Reply first.last3 (701) 1/20/2004 9:28:20 PM

Default User <first.last@boeing.com.invalid> wrote in message news:<400D9D74.FAB7D2F2@boeing.com.invalid>...
> Kifah Abbad wrote:
> > 
>  
> > buf[numbytes] = '\0';
> > printf("Received: %s\n",buf);
> > [/code]
> > 
> > now i know what the first 10 chars of the string in buf is, and i copy
> > it into another variable
> 
> 
> > //extract clearance and copy it into extract_clearance
> > strncpy(extract_clearance,buf,10);
> > //printf("clearnace: %s\n",extract_clearance);
> 
> This is a problem, you didn't null-terminate this new string, strncpy()
> will not do that for you unless the input string was less than the
> number of chars copied. This assumes that num_received is greater than
> 10.
> 
> I prefer:
> 
> extract_clearance[0] = 0;
> 
> strncat (extract_clearance,buf,10);
> 
> 
> > Now...how can i save the rest (what comes after the first 10 chars)
> > into a seocd variable?
> 
> 
> char *rest;
> 
> /*storage for the num received, - the 10 already copied, + null term*/
> rest = malloc (numbytes-10+1);  
> 
> if (rest == NULL)
> {
> /*do error handling*/
> }
> 
> else
> {
>    strcpy (rest, buf+10); 


Thanks guys for the great helpfulness...your ideas were great...now i
need to do some fine tuning to enahnce the performance :-) better
memory handling and stuff
0
Reply kifah (10) 1/21/2004 1:15:58 PM


Kifah Abbad wrote:
> Ok guys,
>  here is the deal, i hope someone can help me out with this.
> 
> I receive a string through a socket...i dunno how long this string is,
> but i save it all into a buf.
> 
> .

You did not provide the declarations of buf or extract_clearance. I 
assume it is like this:

char buf[MAXDATASIZE];
char seocd[MAXDATASIZE];
char extract_clearance[11];

> .
> .
> [code]
> if (!fork()) { // this is the child process
> close(sockfd); // child doesn't need the listener
> if ((numbytes=recv(new_fd, buf, MAXDATASIZE-1, 0)) == -1) {
> 	perror("recv");
> 	exit(1);
> 	

You probably should put another conditional test in the if statement
above to make sure the numbytes is greater than 10.

if((numbytes=recv(new_fd, buf,MAXDATASIZE-1, 0)) = -1 ||
      numbytes <= 10)
{ \* TODO: Handle error *\}

> 
> buf[numbytes] = '\0';

> printf("Received: %s\n",buf);
> [/code]
> 
> now i know what the first 10 chars of the string in buf is, and i copy
> it into another variable
> .
> .
> .
> [code]
> 
> //extract clearance and copy it into extract_clearance
> strncpy(extract_clearance,buf,10);

Nul-terminate extract_clearance.
extract_clearance[10] = '\0';

> //printf("clearnace: %s\n",extract_clearance);
> 
> [/code]
> 
> 
> Now...how can i save the rest (what comes after the first 10 chars)
> into a seocd variable?

strcpy(seocd,buf+10);

-- 
Al Bowers
Tampa, Fl USA
mailto: xabowers@myrapidsys.com (remove the x to send email)
http://www.geocities.com/abowers822/

0
Reply xabowers (230) 1/21/2004 1:24:49 PM

6 Replies
34 Views

(page loaded in 0.327 seconds)

Similiar Articles:













7/12/2012 8:03:05 AM


Reply: