Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

C string manpulation question

Options
  • 06-10-2005 2:55pm
    #1
    Registered Users Posts: 4,222 ✭✭✭


    After years away from it i find myself back doing some C programming :(

    Anywho, heres my problem

    I'm passing in a char * into a function that has a carrige return at the end of it. I need it without it for transmission as part of another string to a socket.

    I've tried the following:

    char text_out[1024];
    int str_length = -1;

    str_length = (strlen(text_in) -1);
    strncpy(text_out, text_in, str_length);
    it looks fine but when i receive it at the socket there is "\CC\CC\CC\CC\CC\" until the end of the text until the end of the char array.


Comments

  • Registered Users Posts: 1,481 ✭✭✭satchmo


    C Strings are NULL-terminated, in other words the last byte of every string is a 0 to show that that's where the string ends. You're removing the last byte of the string, and so removing the terminator - the program won't know where the string ends.

    To remove the carriage return you'll want to remove the last byte and change the second last byte (the return) to 0.


  • Closed Accounts Posts: 3,357 ✭✭✭Beano


    You dont even need to remove the last byte, just do

    text_in[strlen(text_in) -1]=0


  • Registered Users Posts: 1,481 ✭✭✭satchmo


    True - removing the last byte too is just me being anal.


  • Registered Users Posts: 4,222 ✭✭✭Scruff


    Cheers lads and\or lassies!
    That did the trick. Had overlooked that that text_in was an array of chars as well. Ah C strings, they're marvelous :rolleyes:
    Thanks again.


Advertisement