473,398 Members | 2,165 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Extracting first n chars from a string

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.

..
..
..
Expand|Select|Wrap|Line Numbers
  1. if (!fork()) { // this is the child process
  2. close(sockfd); // child doesn't need the listener
  3. if ((numbytes=recv(new_fd, buf, MAXDATASIZE-1, 0)) == -1) {
  4. perror("recv");
  5. exit(1);
  6. }
  7.  
  8. buf[numbytes] = '\0';
  9. printf("Received: %s\n",buf);
  10.  
now i know what the first 10 chars of the string in buf is, and i copy
it into another variable
..
..
..
Expand|Select|Wrap|Line Numbers
  1.  
  2. //extract clearance and copy it into extract_clearance
  3. strncpy(extract_clearance,buf,10);
  4. //printf("clearnace: %s\n",extract_clearance);
  5.  
  6.  

Now...how can i save the rest (what comes after the first 10 chars)
into a seocd variable?
Nov 14 '05 #1
6 5690
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
Nov 14 '05 #2
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
Nov 14 '05 #3
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.

.
.
.
Expand|Select|Wrap|Line Numbers
  1.  if (!fork()) { // this is the child process
  2.  close(sockfd); // child doesn't need the listener
  3.  if ((numbytes=recv(new_fd, buf, MAXDATASIZE-1, 0)) == -1) {
  4.          perror("recv");
  5.          exit(1);
  6.                          }
  7.  buf[numbytes] = '\0';
  8.  printf("Received: %s\n",buf);
  9.  

now i know what the first 10 chars of the string in buf is, and i copy
it into another variable
.
.
.
Expand|Select|Wrap|Line Numbers
  1.  //extract clearance and copy it into extract_clearance
  2.  strncpy(extract_clearance,buf,10);
  3.  //printf("clearnace: %s\n",extract_clearance);
  4.  

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.

--
Er*********@sun.com
Nov 14 '05 #4
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
Nov 14 '05 #5
Default User <fi********@boeing.com.invalid> wrote in message news:<40***************@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
Nov 14 '05 #6


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];
.
.
Expand|Select|Wrap|Line Numbers
  1.  if (!fork()) { // this is the child process
  2.  close(sockfd); // child doesn't need the listener
  3.  if ((numbytes=recv(new_fd, buf, MAXDATASIZE-1, 0)) == -1) {
  4.      perror("recv");
  5.      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);
  •  

  • now i know what the first 10 chars of the string in buf is, and i copy
    it into another variable
    .
    .
    .
    Expand|Select|Wrap|Line Numbers
    1.  //extract clearance and copy it into extract_clearance
    2.  strncpy(extract_clearance,buf,10);
    3.  
    4. Nul-terminate extract_clearance.
    5. extract_clearance[10] = '\0';
    6.  //printf("clearnace: %s\n",extract_clearance);
    7.  


    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: xa******@myrapidsys.com (remove the x to send email)
    http://www.geocities.com/abowers822/

    Nov 14 '05 #7

    This thread has been closed and replies have been disabled. Please start a new discussion.

    Similar topics

    4
    by: lecichy | last post by:
    Hello Heres the situation: I got a file with lines like: name:second_name:somenumber:otherinfo etc with different values between colons ( just like passwd file) What I want is to extract...
    20
    by: hagai26 | last post by:
    I am looking for the best and efficient way to replace the first word in a str, like this: "aa to become" -> "/aa/ to become" I know I can use spilt and than join them but I can also use regular...
    7
    by: Raphi | last post by:
    Hi, I'm trying to clean up a large database in Access. I have one field for address, which needs to be broken up into Street Number, Street Name, and Street Label (St., Road, etc.) The...
    2
    by: Dickyb | last post by:
    Extracting an Icon and Placing It On The Desktop (C# Language) I constructed a suite of programs in C++ several years ago that handle my financial portfolio, and now I have converted them to...
    6
    by: RSH | last post by:
    Hi, I have quite a few .DAT data files that i need to extract the data out of. When i open the files in a text editor I see all of the text that I need to get at BUT there are a lot of junk...
    13
    by: Randy | last post by:
    Is there any way to do this? I've tried tellg() followed by seekg(), inserting the stream buffer to an ostringstream (ala os << is.rdbuf()), read(), and having no luck. The problem is, all of...
    6
    by: Werner | last post by:
    Hi, I try to read (and extract) some "self extracting" zipefiles on a Windows system. The standard module zipefile seems not to be able to handle this. False Is there a wrapper or has...
    1
    by: tempsalah | last post by:
    I need Some Help To Implement a Program That Can Do The Following: 1- Read Text File: kjhg gjhg ghwlifanlkewq flkylkqr dlkfhwelktf jfhehrlknf efhlehflk jhelkjhewlhg 2- Calculate the number...
    6
    by: axapta | last post by:
    Hi Group, How can I check that the first 2 characters of a string are the percent (%) sign? string name = strSurname Regards
    0
    by: Charles Arthur | last post by:
    How do i turn on java script on a villaon, callus and itel keypad mobile phone
    0
    by: emmanuelkatto | last post by:
    Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
    0
    BarryA
    by: BarryA | last post by:
    What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
    1
    by: Sonnysonu | last post by:
    This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
    0
    marktang
    by: marktang | last post by:
    ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
    0
    by: Hystou | last post by:
    Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
    0
    Oralloy
    by: Oralloy | last post by:
    Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
    0
    tracyyun
    by: tracyyun | last post by:
    Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
    0
    isladogs
    by: isladogs | last post by:
    The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

    By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

    To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.