开发者

c string version of atoi

is there any functions like atoi read the buffer to return a string?

fgets(input, data_len, stdin);
return atoi(input + 6); // i开发者_StackOverflow need the string

thanks

full code

char *getInput2(char *param) {
    data_len=atoi(getenv("CONTENT_LENGTH"));
    char input[data_len];
    fgets(input, data_len, stdin);

    char *r_str;
    strcpy (r_str, input+6);

    return r_str;
}

finally, internal server error........

char *getInput2(char *param) {

    char input[100];
    data_len=sizeof(input);
    fgets(input, data_len, stdin);


    return strdup (input+7);
}

only can return first char......enter 12 only return 1, what's the problem?


I'm not entirely certain what you're asking but if it's how to get the string representation of the integer at offset 6, you can do it as a two-step:

char str[enough_to_hold_datalen_and_then_some];
int val = atoi (input+6);
sprintf (str, "%d", val);

Alternatively, if you want to get a chunk of the string regardless of whether it's made up of digits:

strcpy (str, input+6);                      // get the rest of the string
strncpy (str, input+6, 4); str[4] = '\0';   // get up to four characters.

If your problem is that input is a local variable to the function and, when you return its address, you get bogus data because it's gone out of scope, I'd use:

return strdup (input+6);

That will return a copy of the string on the heap (which is long-lived as opposed to the stack frame, which is not).

Just remember that you need to free that memory when you're finished with it. And be certain that you actually have six characters in that buffer.

If your C implementation doesn't have a strdup, use this one.


Based on your update, it looks like you're after that last option. With your code:

char *r_str;
strcpy (r_str, input+6);
return r_str;

you are not actually allocating any storage to put the string copy (hence the crash). I would replace that whole bit with a simple:

return strdup (input+6);

as I suggested.


Ha, ha, gotta love those Linux man page writers:

If the destination string of a strcpy() is not large enough (that is, if the programmer was stupid/lazy, and failed to check the size before copying) then anything might happen. Overflowing fixed length strings is a favourite cracker technique.


Assuming you want to return the string, then atoi has nothing to do with it. Instead, your problem is that you cannot return memory of a local variable. Returning memory in C is always difficult; typically, you return dynamically allocated memory and expect the caller to free it:

char* get_value()
{
    char input[100];
    int data_len = sizeof(input);
    char *result;
    fgets(input, data_len, stdin);
    result = malloc(strlen(input)-5); /* reserve enough memory, including the terminating NUL */
    strcpy(result, input+6);
    return result;
}

(you can shorten this by using strdup)

Alternatively, you can use a global variable, but that won't be reentrant:

char input[100];
char* get_value()
{
    int data_len = sizeof(input);
    fgets(input, data_len, stdin);
    return input+6;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜