question about reading an input using fgets and scanf in c [duplicate] - c

This question already has answers here:
fgets doesn't work after scanf [duplicate]
(7 answers)
Closed 3 years ago.
I have recently been trying to do an exercise in c.
I want to read the input which is something like: "SET 0" (note that the actual text will be parsed later).
I tried fgets as it is like that:
char in[20];
//ok, this reads the first line, the first input is meant to be a number
scanf("%s",in);
if(isdigit(in[0])){
char array[]={'?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?'};
auto counter = atoi(in);
while(counter !=0){
fgets(in, sizeof(in),stdin);
For some reason, when i type eg "SET 0" and i am using fgets the in variable is empty(will print nothing).
I tried scanf but it will not read the number.
Any ideas/suggestions of what i can do?
Thanks in advance!

Do not mix fgets() with scanf() until you know why scanf()` is evil.
fgets() read the left over '\n' of the first line of user input which scanf() did not read.

Related

Reading entire line from console [duplicate]

This question already has answers here:
How to read from stdin with fgets()?
(6 answers)
Closed 2 years ago.
void name(record *el)
{
char k[100];
printf("Name: ");
fgets(k,100,stdin);
printf("\n");
}
I'm trying to write a function that reads a line from console and then searches for it in a list. I'm having some problem with reading a whole line. The program just skips the fgets line. The same happens if i try with scanf("%[^\n]%*c").
You probably read an integer before calling the function and the '\n' was left on the input stream.
Try calling the function as the first command you do in main and you will see that it works.
If you read a number right before the call with scanf for example, you could add a '\n' in the parameter string before calling the fgets:
scanf("%d\n", &x);

fgets() function doesn't recognise the data [duplicate]

This question already has answers here:
strcmp on a line read with fgets
(6 answers)
Closed 3 years ago.
I'm currently building my own shell.
I managed to make ls function using scanf() and now I'm on cd. But I realized that cd has space between cd and file so I changed scanf() to fgets().
The space problem has been solved but another problem came out. Even though I typed correctly, the program won't work.
char command[MAX_LEN];
int result = 1;
char *pwd[MAX_LEN];
do {
printf("%s > ", getcwd(pwd, 100));
fgets(command, 100, stdin);
if(!strcmp("cd", command))
change_dir();
if(!strcmp("ls", command))
list_dir();
} while(strcmp("exit", command));
return 0;
What is wrong with my code? Can you please tell me why it's happening?
fgets reads up to size-1 chars (in your case 100-1 so 99) and places a '\0' at the end of the string, so when you type "ls" in stdin and hit enter, fgets will receive "ls\n" since enter creates a \n char, and the command will be "ls\n\0"
since the \0 indicates the end of the string we can ignore this here for now.
When you strcmp("ls",command), you are comparing "ls" with the string in command that ends before \0 so "ls\n" since "ls" and "ls\n" are not the same, it will fail.
As others already have said, you could either add \n to your compare strings, or remove the \n
since you may be planning to add more to the line after your cd or ls, you could look into string seperation e.g. with strtok()

Have scan before a fget [duplicate]

This question already has answers here:
What can I use for input conversion instead of scanf?
(9 answers)
Closed 3 years ago.
I use a
scanf("%d%d",&row,&col); // row & col is int
before
fgets(buf, MAXLEN, stdin); //MAXLEN == 65535 ; buf is char
I want to input buf after input row and col
but fgets will be skip
May someone help me?
This issue can arise if your input is on two separate lines. scanf() will leave a newline character in the buffer; when fgets() tries to read the remainder of the input, it will receive the newline character and stop reading. The following may fix your code:
scanf("%d%d\n", &row, &col);
This will read the newline in as well, leaving the rest for fgets().

Get words which contains space in c [duplicate]

This question already has answers here:
Simple C scanf does not work? [duplicate]
(5 answers)
Closed 7 years ago.
I need to get words from user which contains space as I expressed at title with struct statement.
For example :
#include <stdio.h>
struct knowledge
{
char name[30];
}person;
int main()
{
scanf("%s",person.name);
printf("\n\n%s",person.name);
}
When I run this program and enter a sentence like "sentence" there is no problem. It show me again "sentence".
However, when I enter "sentence aaa" it shows me just first word ("sentence"). What is the matter here? Why it doesn't show me all ("sentence aaa") I entered?
Instead of scanf() use
fgets(person.name,sizeof(person.name),stdin);
It is always a bad idea to use scanf() to read strings. The best option is to use fgets() using which you avoid buffer overflows.
PS: fgets() comes with a newline character
%s format specifier stops scanning on encountering either whitespace or end of stream. hence, you cannot input a "sentence" with space using scanf() and %s.
To scan a "whole line" with space, you need to use fgets(), instead.

How to stop a program from storing more than a single character into a char variable? [duplicate]

This question already has answers here:
How to prevent scanf causing a buffer overflow in C?
(6 answers)
Closed 8 years ago.
My program uses a scanf as such:
scanf ("%c", &symbol);
is there a way to print an error if the user enters in a string > one character? e.g "abc" as it messes with the program later on
Use a string buffer, fgets() into it, check if the second character is a \n.

Resources