parsing a string of chars

This is a discussion on parsing a string of chars within the C Programming forums, part of the General Programming Boards category; Hello, I would appreciate any suggestions. I have a client and server program. The server writes a struct to a ...

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    8

    parsing a string of chars

    Hello,

    I would appreciate any suggestions. I have a client and server program. The server writes a struct to a socket:

    Code:
    struct packet
    {
            int seq;
            int type;
            int size;
            char data[1024];
            unsigned long int checksum;
    };
    the server writes a struct to the socket as a long array of chars 1034 bytes long...

    now the client reads the packet into a char buffer i've allocated.

    now i want to parse all the respective fields from the buffer. i am thinking of using:

    Code:
    struct packet current;
    
    sscanf(buffer, " %i %i %i %1024c %uli", &current.seq, &current.type,  &current.size,  current.data, &current.checksum);
    will/should this work in the way i have it written? i tried it this was and read FFFFFFFF for size, and for the data as well. don't quite know why.

    i know that i could do a series of memcpy's to copy the fields, but is there any easier way to do this?

    thanks in advance!!

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,159
    sscanf() is expecting ASCII data, but you're passing binary data to it. You can either send the fields as ASCII or use something like memcpy() like you considered for storing it.

    Unless I'm misunderstanding the method you're using to send the data that is. Can you show us what that looks like?
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    8
    i am using binary data. darn i thought there would be an easier way than using memcpy's.

    anyone know of any functions that will do everything in one step?

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,159
    If you're doing something like write(sock, mystruct, sizeof(mystruct)); to send the data then you can just do read(sock, mystruct, sizeof(mystruct)); on the receiving end. Assuming the server and client are of the same architecture anyway.
    If you understand what you're doing, you're not learning anything.

  5. #5
    Registered User
    Join Date
    Mar 2005
    Posts
    8
    yesss i had the same idea. thanks. i will try that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 02:40 AM
  2. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 09:33 PM
  3. Counting how many chars in a string
    By tigs in forum C Programming
    Replies: 4
    Last Post: 08-05-2002, 12:25 AM
  4. ........ed off at functions
    By Klinerr1 in forum C++ Programming
    Replies: 8
    Last Post: 07-29-2002, 09:37 PM
  5. string handling
    By lessrain in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 07:36 PM

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