2

I will start with my code:

char input[40];
fgets( input, 40, stdin );
if( checkPalin(input) == 0 ) {
    printf("%s ist ein Palindrom \n", input);
}
else {
    printf("%s ist kein Palindrom \n", input);
}

What I want to do is: Read some standardinput and check with my function if it is a Palindrome or not.

My problems are the following: How can I get the length of the standardinput? Because if it is larger then 40 chars I wanna put an errormessage and furthermore I want my char array to be the exact length of the actual input.

Anybody can help me?

glglgl
  • 89,107
  • 13
  • 149
  • 217
Jakob Abfalter
  • 4,980
  • 17
  • 54
  • 94

4 Answers4

1

With

fgets( input, 40, stdin );

input is guaranteed to have number of characters less than equal to 40 (null termination included)

You don't have to perform checks .

And for getting size of the input you can always use strlen() function on input, as the produced character string from fgets is always null terminated.

P0W
  • 46,614
  • 9
  • 72
  • 119
  • but if for example the stdin is like 45 charakters. In this case I don`t want to just get 40 chars and than cut, I want to put an error message. How can can I do that? – Jakob Abfalter Oct 26 '13 at 18:04
  • @JakobAbfalter then probably you need to write your own version of `fgets`, taken inputs as characters and form C string, if number of char increase than max size, display message. I also, won't suggest `gets`, because it is unsafe, vulnerable to buffer overflow – P0W Oct 26 '13 at 18:08
1

There's no any function to do it, you need to write it yourself. I.e., read byte by byte looking for EOF character. But I guees you're doing it for avoid overflow, right? if input is larger than 40 characters, you don't need to because is guaranted such a extra values is not put into your buffer by fgets() function, it's never larger than the size you have requested: 40. The value may be less-than or equal, but never greater than.

EDIT:

By "How to get the lenght of a standardinput in C?" I was thinking that you're talking about how many bytes there's in stdin. I'm sorry for that. If you want to get how may bytes has fgets() written in, just use strlen()

The Mask
  • 17,007
  • 37
  • 111
  • 185
1

fgets( input, 40, stdin );

length of input should not go beyond 40 characters == 39characters + nul character

If you give string having length more than 39 characters, then fgets() reads first 39 characters and place nul character('\0') as 40 character and ignores remaining characters.

If you give string less than 39 characters , for example 5 then it places reads newline also length becomes 6(excluding nul character)

Do not forgot to remove newline character.

char input[60];
fgets(input,sizeof input,stdin);

For example if you declare input buffer size with some 60 then if you want to do error checking for more than 40 characters.

You can simply check with strlen() and check length is more than 40.then show error message

If you want to check error with fgets() check against NULL

Gangadhar
  • 10,248
  • 3
  • 31
  • 50
1

It just turned out that it is not so easy to write a function which uses fgets() repeatedly in order to return a malloc()ed string.

The function does no proper error reporting: If there was an error using realloc() or fgets(), the data retrieved till now is returned.

Apart from these, the function proved quite usable.

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

char * read_one_line(FILE * in)
{
    size_t alloc_length = 64;
    size_t cumulength = 0;
    char * data = malloc(alloc_length);
    while (1) {
        char * cursor = data + cumulength; // here we continue.
        char * ret = fgets(cursor, alloc_length - cumulength, in);
        printf("r %p %p %zd %zd %zd\n", data, cursor, cumulength, alloc_length, alloc_length - cumulength);
        if (!ret) {
            // Suppose we had EOF, no error.
            // we just return what we read till now...
            // there is still a \0 at cursor, so we are fine.
            break;
        }
        size_t newlength = strlen(cursor); // how much is new?
        cumulength += newlength; // add it to what we have.
        if (cumulength < alloc_length - 1 || data[cumulength-1] == '\n') {
            // not used the whole buffer... so we are probably done.
            break;
        }
        // we need more!
        // At least, probably.
        size_t newlen = alloc_length * 2;
        char * r = realloc(data, newlen);
        printf("%zd\n", newlen);
        if (r) {
            data = r;
            alloc_length = newlen;
        } else {
            // realloc error. Return at least what we have...
            // TODO: or better free and return NULL?
            return data;
        }
    }
    char * r = realloc(data, cumulength + 1);
    printf("%zd\n", cumulength + 1);
    return r ? r : data; // shrinking should always have succeeded, but who knows?
}

int main()
{
    char * p = read_one_line(stdin);
    printf("%p\t%zd\t%zd\n", p, malloc_usable_size(p), strlen(p));
    printf("%s\n", p);
    free(p);
}
glglgl
  • 89,107
  • 13
  • 149
  • 217