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.
..
..
.. -
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
..
..
.. -
-
//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? 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
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
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.
. . . - 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 . . . - //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.
-- Er*********@sun.com
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
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
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];
. . - 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);
-
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);
-
-
Nul-terminate extract_clearance.
-
extract_clearance[10] = '\0';
-
- //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);
--
Al Bowers
Tampa, Fl USA
mailto: xa******@myrapidsys.com (remove the x to send email) http://www.geocities.com/abowers822/ This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
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...
|
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...
|
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,...
|
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...
|
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,...
|
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...
|
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...
| |