Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello.
I want to give an input via fgets() ,but I'm in trouble.

For example, I give :

hello and I receive ello
I give hello world I receive ello world
How to solve it?

Thanks!!!


Code:

What I have tried:

C++
<pre>#include <stdio.h>
#include <stdlib.h>


char *input();

int main(void)
{
    while(1)
    {
        char *a = input();
        printf("%s..." , a);
    }
}

char *input()
{
    printf("Give: ");
    char *word = malloc(sizeof(char) * 50);
    fgetc(stdin);   
    fgets(word , 100 , stdin); 
    return word;
}




I have change the code a little bit (in my basic program (that's was just a test)) ,and now is like this:


C++
char *word = malloc(sizeof(char) * 100);

fgets(word , sizeof(word) , stdin);   /
while (fgets(word,sizeof(word),stdin) != NULL)
{
    break;
}
size_t len = strlen(word) - 1;
if (word[len] == '\n') //without newline
{
    word[len] = '\0';
}
return word;



It works perfect for simple words ,but when I give a sentence ,for example :
hello world ,I receive hello w (without "orld").......
Posted
Updated 26-Sep-20 8:10am
v4

Quote:
hello and I receive ello

C++
char *input()
{
    printf("Give: ");
    char *word = malloc(sizeof(char) * 50);
    fgetc(stdin);   // may be this is what eating the first letter
    fgets(word , 100 , stdin); 
    return word;
}

[Update]
Try to replace:
C++
fgets(word , sizeof(word) , stdin);

with:
C++
fgets(word , 100 , stdin);

word is a pointer, its size is the size of the pointer, not the size of space it is pointing to.
In C/C++, arrays have np knowledge of their own size.
 
Share this answer
 
v2
Here's how I would write that code :
C++
void input( char buffer, size_t bufferSize )
{
    char * p;
    printf("Give: ");
    fgets( buffer, bufferSize, stdin );
    p = strchr( buffer, '\n' );
    if( p )
       *p = 0;     // null new line character if found
}

#define BUFFER_SIZE  79

int main(void)
{
    char buffer[ BUFFER_SIZE + 1 ] = { 0 };   // +1 for terminating null
    while(1)
    {
        input( buffer, BUFFER_SIZE );         // leave room for the null
        printf( "input was '%s'\n" , buffer );
        if( stricmp( buffer, "exit" ) == 0 )
            break;                            // give a way to escape the loop
    }
    return 0;
}
 
Share this answer
 
Comments
Nick_is_asking 26-Sep-20 15:07pm    
I test it and it works, BUT when I try to do it in my basic code I receive: Segmentation fault (core dumped)

char *get_word(char *buffer , size_t bufferSize , char *player_A , char *player_B , int flag)
{

char *p;

//fgetc(stdin);
fgets( buffer, bufferSize, stdin );
p = strchr( buffer, '\n' );
if( p )
{
*p = 0;
}


return buffer;
}

In main I have this: char word[ 100 + 1 ] = { 0 }; and I call
char *str = get_word(word , 100 , A , B , 0);
but doesn't work...
Rick York 26-Sep-20 15:39pm    
You are passing A and B in but nothing is done with them so why are you passing them?
Why bother returning the address of the buffer passed in when it is already known?
Nick_is_asking 26-Sep-20 15:49pm    
Because I want to return this character to others functions(Program is big).
I tried MANY methods ,but still nothing...
No,it doesn't work.
I have change the code a little bit (in my basic program (that's was just a test)) ,and now is like this:
C++
char *word = malloc(sizeof(char) * 100);

fgets(word , sizeof(word) , stdin);   /
while (fgets(word,sizeof(word),stdin) != NULL)
{
    break;
}
size_t len = strlen(word) - 1;
if (word[len] == '\n') //without newline
{
    word[len] = '\0';
}
return word;


It works perfect for simple words ,but when I give a sentence ,for example :
hello world ,I receive hello w (without "orld").......
 
Share this answer
 
Comments
Richard MacCutchan 26-Sep-20 11:29am    
fgets(word , sizeof(word) , stdin);
The sizeof operator is a compile time operation, and gives the size of the object referred to. In your case sizeof(word) will give the answer 4, since word is a pointer. So you are telling fgets that your buffer is only 4 bytes wide.

[edit]
I guess you are building 64-bit as the size of your pointer would appear to be 8 bytes.
[/edit]
Nick_is_asking 26-Sep-20 11:32am    
Thanks for the update.
so ,what can I do?
Richard MacCutchan 26-Sep-20 11:38am    
Get a book on the C language.

But the quick fix to your code is:
#define BUFLEN 100
char *word = (char*)malloc(sizeof(char) * BUFLEN);

fgets(word, BUFLEN, stdin);
while (fgets(word, BUFLEN, stdin) != NULL)
{
// what is this loop for?
}
Nick_is_asking 26-Sep-20 11:55am    
I'm a college student.I will have C this semester,so they will give us book.
As about this loop ,I found it on internet.
Richard MacCutchan 26-Sep-20 12:03pm    
Stop wasting your time copying code from the internet which you do not understand. It will only serve to confuse you when you try to learn the language properly.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900