Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C programming: print only int from fgets

See this main:

int main(void)
{
    int i;
    int ch;
    char str[512];
    fgets(str, sizeof str, stdin);

    for (i = 0; i <= (strlen(str)); i++)
    {
        if (str[i] != '\0' && str[i] != '\n')
        {
            int num = atoi(&str[i]);
            printf("%d\n", num);
        }
    }

    return 0;
}

I want to get line with numbers from user and get all the numbers without any spaces or tabs.

For example:

The input 1 2 3. But in this case this the output:

1
2
2
3
3

So why i received 2 and 3 twice?

like image 305
user2908206 Avatar asked Nov 23 '17 09:11

user2908206


People also ask

Can you use fgets for int in C?

Simple integer input in C, using `fgets()` to get the entire line as a string and `strtol()` to parse the integer.

What does fgets return?

Return Value The fgets() function returns a pointer to the string buffer if successful. A NULL return value indicates an error or an end-of-file condition. Use the feof() or ferror() functions to determine whether the NULL value indicates an error or the end of the file.

What's the difference between gets () and fgets ()?

Even though both the functions, gets() and fgets() can be used for reading string inputs. The biggest difference between the two is the fact that the latter allows the user to specify the buffer size. Hence it is highly recommended over the gets() function.

Does fgets print newline?

fgets terminates at the newline character but appends it at the end of the string str . The function also appends the terminating null character at the end of the passed string.

How do you use fgets in C?

fgets() Function in C. The syntax of the fgets() function is: Syntax: char *fgets(char *str, int n, FILE *fp); The function reads a string from the file pointed to by fp into the memory pointed to by str.

What is the difference between fprintf () and fgetchar ()?

fgets () function reads string from a file, one line at a time. fputs () function writes string to a file. feof () function finds end of file. fgetchar () function reads a character from keyboard. fprintf () function writes formatted data to a file. fscanf () function reads formatted data from a file.

What is the use of int fgetc pointer?

int fgetc (FILE *pointer) pointer: pointer to a FILE object that identifies the stream on which the operation is to be performed. The entire content of file is printed character by character till end of file. It reads newline character as well. fputc () is used to write a single character at a time to a given file.

What is the declaration for fgets () function in Python?

Following is the declaration for fgets () function. str − This is the pointer to an array of chars where the string read is stored. n − This is the maximum number of characters to be read (including the final null-character).


2 Answers

Here's how I would do it:

char line[256];
if (fgets(line, sizeof line, stdin) != NULL)
{
    const char *ptr = line;
    while (*ptr != '\0')
    {
        char *eptr = NULL;
        const long value = strtol(ptr, &eptr, 10);
        if (eptr != ptr)
            printf("%ld\n", value);
        else
            break;
        ptr = eptr;
    }
}

This uses strtol() so it will also handle negative numbers; if this is incorrect you can of course add checks to filter them out. I think this is way better than anything using strtok().

like image 191
unwind Avatar answered Sep 21 '22 20:09

unwind


Because the you also pass the position of the string which starts with spaces. they get the first number to be 2 and 3 respectively twice. That is what is returned.

for (i = 0; i <= (strlen(str)); i++)
{
    if (str[i] != '\0' && !isspace(str[i]) )
    {
        int num = atoi(&str[i]);
        printf("%d\n", num);
    }
}

Prints:

1
2
3

For the purpose of tokenizing you can use strtok and to convert it to number strtol etc. These provides much better control over the error cases than atol/atoi do.

like image 23
user2736738 Avatar answered Sep 24 '22 20:09

user2736738