Removing 'strcat' ed string.

This is a discussion on Removing 'strcat' ed string. within the C Programming forums, part of the General Programming Boards category; #include <stdio.h> #include <stdlib.h> int main() { char str1[27]="a"; char str2[2]; int n; str2[1]= 0; for (n = 'a' ; ...

  1. #1
    Unregistered
    Guest

    Question Removing 'strcat' ed string.

    #include <stdio.h>
    #include <stdlib.h>

    int main()
    {
    char str1[27]="a";
    char str2[2];
    int n;

    str2[1]= 0;

    for (n = 'a' ; n < 'j' ; n++)
    {
    str2[0] = n;
    strcat(str1, str2);
    printf( "%s\n ", str1);
    }
    }

    'strcat' ing is fun but situation become worse when you need to remove the 'strcat' ed string. The above code is just a simulation of my actual code. I had used strcat to make a header of packets. Of course at the receiver end, you have to remove the header. How do I actually do it? I tried to remove the header by just ignoring the first few bytes which represents the header, it's ok to do so? But I found disadvantage by doing so because if I ignored the header, then the computer will not know which packets is which! Please help!

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    This is easy. How many bytes is your header?

    strcat( buffer, packet_string+headersize );

    Just skip past it, and strcat it on your buffer or whatever.

    I'm not sure what the actual problem is. Read the packets and store them in a sorted linked list. Do something like this:
    Code:
    struct PacketList {
        struct PacketList *prev;
        struct PacketList *next;
        int packetnumber;
        unsigned char *data;
    };
    Now then, just rip your packets apart as they come in, and stick
    them into a 'PacketList' structure, tagging what packet number they are, and order them in the linked list of packets.

    Additionally, you could just add another variable to this, and track
    a start and stop packet number. Then, you could basicly say that
    'PacketList' one contained packets 1-4, all concatenated(sp?), and
    say that packet 5 never shows up, so you start 'next' to have the
    next block of packets. When you find out that 5 arrives, you mash
    them all into packet 1.

    It's a bit more work, but it's could be done.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Unregistered
    Guest

    Question

    >>strcat( buffer, packet_string+headersize );
    >>Just skip past it, and strcat it on your buffer or whatever.

    Why do we need to strcat that packet_string+headersize into another buffer since it is already 'strcat'ed with a header?


    I used 2 bytes to represent the header. One is for sequence of the files and the other is for sequence of packets.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I was assuming at this point you were decoding the incoming packets. Basicly, what I was doing, was discarding the packed, because we don't need it, IMO, and creating the "full version" of whatever the series of packets contain.

    Additionally, you could just store packets in a linked list, and when you want to combine them, just use strncat() or strncpy() to create the final product.

    strncat(final_product, node->data+headersize, node->dataLength );

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 05:05 AM
  2. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 04:13 PM
  3. problems with overloaded '+' again
    By Brain Cell in forum C++ Programming
    Replies: 9
    Last Post: 04-14-2005, 06:13 PM
  4. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2003, 12:03 AM
  5. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21