-1

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

fgets(input, data_len, stdin);
return atoi(input + 6); // i 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?

friends
  • 589
  • 5
  • 10
  • 16
  • I don't understand the question – Armen Tsirunyan Nov 01 '10 at 08:23
  • 1
    This question is very unclear. Please explain what you want to achieve. – Björn Pollex Nov 01 '10 at 08:24
  • 2
    Here is my theory: he wants to return a substring of the input (input+6). This fails because it's a local variable. He knows he can return the value successfully as an int, using atoi. Now he wants the same for strings (i.e. strings-by-value). He doesn't know that these really don't exist in C. The most literal answer would be "strdup". – Martin v. Löwis Nov 01 '10 at 08:29
  • Can you give an example of the input and the output you would expect? Your question isn't very clear. Something like getInput2("hello world") should return " world" would be very helpful. – pinkfloydx33 Nov 01 '10 at 08:43
  • In support of Martin's interpretation, note his earlier question [c pass a string to function then return a string](http://stackoverflow.com/questions/4066873/). – dmckee --- ex-moderator kitten Nov 03 '10 at 03:47

2 Answers2

2

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.

Community
  • 1
  • 1
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
0

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;
}
Martin v. Löwis
  • 124,830
  • 17
  • 198
  • 235