Thread: My string gets random extra characters

  1. #1
    Registered User
    Join Date
    Dec 2015
    Posts
    26

    My string gets random extra characters

    I keep searching and not being able to find what the problem is and it's driving me crazy... specially since its probably something so simple.

    I'm making a FTP client in C for a school project and for now everything is going well apart from one little thing.

    I'm working on a function to isolate the return code of the server for example:

    220 Personal FTP

    The function would return 220 as an integer.

    Code:
    int returnCode(char * buf) {
        printf("----%s",buf);
        char txt[3] = "123";
        //strncpy(test,buf,3);
        printf("----%s",txt);
        //long i = strtol(&buf[1],text,20);
        //printf("%ld",i);
        return 0 ; /* value for compile*/
    };
    I commented some of the stuff because already for now just when i printf the txt string it gives me this: My string gets random extra characters-wtf-png

    could anyone help me?

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Code:
    char txt[3] = "123";
    This declares a character array of three characters.

    txt[0] = '1'
    txt[1] = '2'
    txt[2] = '3'

    Code:
    printf("----%s",txt);
    This attempts to print a string called "txt".

    But ... your array is not a string. A string in C is an array of characters terminated by a null character ('\0').

    You didn't give enough room in your array for the compiler to store the string-terminating null character.

    Try changing it to:

    Code:
    char txt[4] = "123";
    Now you have a spot for the '\0' to be stored. And that's what makes it a string.

  3. #3
    Registered User
    Join Date
    Dec 2015
    Posts
    26
    thank you it works like a charm. I knew it was a really stupid mistake i had forgotten.
    One last thing if you dont mind would you know why my strtol is not working properly
    Code:
    int returnCode(char * buf) {
        char txt[4] = "123";
        char *ptr;
        printf("----%s",buf);
        strncpy(txt,buf,3);
        printf("----%3s",txt);
        long i = strtol(txt,&ptr,4);
        printf("\n%li\n",i);
        return 0 ; /* value for compile*/
    };
    it returns 0 all the time in the printf.

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    That depends on (1) what the value of "buf" is, and (2) why you selected the third argument of "strol()" to be 4.

    Perhaps you should post the simplest complete program that compiles and illustrates the problem, along with input and expected output.

    And if a function does not need to return a value, just declare it as returning void.

  5. #5
    Registered User
    Join Date
    Sep 2015
    Location
    Australia
    Posts
    63
    Hi...

    Looking at the strncpy function...you would be cpying 3 char....and yet again missing the '\0' char. which could cause you problems..
    As mentioned earlier...if you had of tested the strtol() by trying to print the "123" you would have found 4 would not work, but 10 would have....as its a base number in that functon...
    Everything assumes the data in buf is correct...

  6. #6
    Registered User
    Join Date
    Oct 2015
    Posts
    28
    You'll want to avoid strncpy for copying strings, its main use is for copying zero-padded arrays of a fixed length, which is not too common nowadays. :-p

    To copy a string, just use strcpy:

    Code:
    strcpy(txt, buf);
    As this may write past the end of txt, check that it won't beforehand, and handle the case where it will:

    Code:
    if (strlen(buf)+1 > sizeof txt)
            return -1;  // buf won't fit into txt
    (In this situation, you don't actually need to make a copy of the string, you can just pass buf straight to strtol. But please read the documentation for strtol, the third argument in your call seems incorrect, see this URL: <http://port70.net/~nsz/c/c11/n1570.html#7.22.1.4>)

  7. #7
    Registered User
    Join Date
    Dec 2015
    Posts
    26
    The strncpy works fine but the conversion to integer doesnt work.

    Basically what im trying to do is my input to the function is under this format:

    Code:
    220 Personal FTP
    or
    Code:
    530 Login or password incorrect!
    I want to simply isolate and return the integer (220 or 530)

    for now this function manages to isolate the number but only as a string (txt)

    Code:
    int returnCode(char * buf) {
        char txt[4] = "123";
        char *ptr;
        printf("----%s",buf);
        strncpy(txt,buf,3);
        printf("----%3s",txt);
        long i = strtol(txt,&ptr,4);
        printf("\n%li\n",i);
        return 0 ; /* value for compile*/
    };
    here the value of txt is perfect but whenever i use strtol i becomes 0 instead of becoming that number. what am i missing in the strtol to make it work.

  8. #8
    Registered User
    Join Date
    Dec 2015
    Posts
    26
    My bad i didnt see the last argument was base i thought it was lenght i changed it to:
    Code:
    long i = strtol(txt,&ptr,0);
    And it works perfect!

    Thanks for all your help you can close the thread

  9. #9
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I am fairly certain that strtol only works properly when the string argument is a proper C string; that is, "a sequence of characters terminated by an ASCII zero." This is the standard C string definition, so I don't see much point in fighting it. You will need to work out the code to feed strtol its proper arguments.

    Using an array that wasn't so ridiculously small is one way to fix your problem. I know it's not the most efficient, but it's better than the result being wrong.

  10. #10
    Registered User
    Join Date
    Sep 2015
    Location
    Australia
    Posts
    63
    Hi ..

    As far as I can see you are missing nothing in the strtol() fuction....I ran the code and got a number everytime, also got "123" when I tested that , but only when changed 4 to 10.... what header includes are you using ? OK, 0 is a special and will also work...but the other issues remain.
    Last edited by JohnGM; 12-08-2015 at 06:39 AM.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You should consider the off-chance that what you get from the server may be malformed. As such consider something like this:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int extractReturnCode(const char *text, int *result)
    {
        char *endptr;
        *result = strtol(text, &endptr, 10);
        return endptr - text == 3; /* return codes are always 3 characters in length */
    }
    
    int main(void)
    {
        int return_code;
        if (extractReturnCode("220 Personal FTP", &return_code))
        {
            printf("%d\n", return_code);
        }
        else
        {
            fprintf(stderr, "There was an error in extracting the return code.\n");
        }
        return 0;
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Quote Originally Posted by laserlight View Post
    You should consider the off-chance that what you get from the server may be malformed. As such consider something like this:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int extractReturnCode(const char *text, int *result)
    {
        char *endptr;
        *result = strtol(text, &endptr, 10);
        return endptr - text == 3; /* return codes are always 3 characters in length */
    }
    
    int main(void)
    {
        int return_code;
        if (extractReturnCode("220 Personal FTP", &return_code))
        {
            printf("%d\n", return_code);
        }
        else
        {
            fprintf(stderr, "There was an error in extracting the return code.\n");
        }
        return 0;
    }
    You're making things way too complicated.

    https://www.youtube.com/watch?v=BovQyphS8kA

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extra characters with
    By computerquip in forum C Programming
    Replies: 4
    Last Post: 09-29-2010, 03:57 PM
  2. Extra Characters
    By david84 in forum C++ Programming
    Replies: 4
    Last Post: 09-23-2010, 10:41 AM
  3. Extra characters in string
    By Tigers! in forum C Programming
    Replies: 10
    Last Post: 07-28-2009, 11:48 PM
  4. Flushing extra characters
    By Mingzhi in forum C++ Programming
    Replies: 1
    Last Post: 02-22-2004, 02:25 PM
  5. Handling Extra Characters
    By hern in forum C Programming
    Replies: 3
    Last Post: 02-21-2004, 10:40 PM