Extracting first n chars from a string

K

Kifah Abbad

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);

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);


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

Eric Sosman

Kifah said:
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);

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);

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.
 
H

Hallvard B Furuseth

Kifah said:
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';
 
J

Jirka Klaue

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
 
D

Default User

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
 
K

Kifah Abbad

Default User said:
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
 
A

Al Bowers

Kifah said:
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);
	[/QUOTE]

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 *\}
[QUOTE]
buf[numbytes] = '\0';[/QUOTE]
[QUOTE]
printf("Received: %s\n",buf);

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);[/QUOTE]

Nul-terminate extract_clearance.
extract_clearance[10] = '\0';
[QUOTE]
//printf("clearnace: %s\n",extract_clearance);


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

strcpy(seocd,buf+10);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,137
Messages
2,570,797
Members
47,342
Latest member
eixataze

Latest Threads

Top