Have scan before a fget [duplicate] - c

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().

Related

Unable to run a piece of code to get different types of data from user in C [duplicate]

This question already has answers here:
scanf() leaves the newline character in the buffer
(7 answers)
Closed 2 years ago.
This question might look stupid but I am not used to work with C and I'm losing my mind here not knowing what is wrong.
Basically what I want is to get a number from the user, then a string, the user may write whatever he feels like, I want to keep just the first caracter of that string.
My code is the following:
#include <stdio.h>
int main()
{
int b, n;
char frase [2];
scanf("%d", &n);
fgets(frase, 2, stdin);
puts(frase);
return 0;
}
My problem is, after the fgets the program stops, no matter what I have after it, it only stops, no error messages or anything. What is happening?
What is happening?
Your code will:
For an input 123 abc:
Store 123 in n, store the space in frase followed by a null byte. Then it will print that single space and end its execution with no errors.
For an input 123 Enter abc:
Store 123 in n and store in frase the newline character \n added to the buffer when you pressed Enter, followed by a null byte, next it will print that \n and end its execution with no errors.
So it doesn't just stop, it does what it's supposed to do.
What you shoud do, to make your code more robust, is to also parse the number with fgets, and convert it with sscanf or strtol:
int n = 0;
char buffer[20];
char frase[2];
fgets(buffer, sizeof buffer, stdin);
if(sscanf(buffer, "%d", &n) != 1){
//value not parsed
}
fgets(frase, sizeof frase, stdin);
puts(frase);
Using scanf to parse inputs is rarely, if ever, a good ideia.
The fgets is grabbing the newline character that is in the buffer after scanf reads the int. Putting a getchar() before your fgets should fix the problem.

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

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.

In this super easy program written in C won't let me read a variable of type "char"? [duplicate]

This question already has answers here:
can not read character from user [duplicate]
(2 answers)
scanf() leaves the newline character in the buffer
(7 answers)
Closed 4 years ago.
This easy code in C won't work, and I don't get it why. If I read only "n", or only "ch" separately, it works, otherwise if I try to read them both, it won't let me read "ch". What happens and how could I make it work?
#include <stdio.h>
int main()
{
int n;
char ch;
printf("n=");
scanf("%d",&n);
printf("ch="); //when i press Build and Run it won't let me read "ch"??? why?
scanf("%c",&ch);
return 0;
}
When you read in a number using %d, a newline is left in the input buffer. When you then read a character with %c, it reads that newline immediately so you don't get prompted for more input.
Unlike the %d format specifier, which discards any leading whitespace, the %c format specifier does not.
Add a leading space before %c to consume any leftover whitespace:
scanf(" %c",&ch);

C - How to read a string with only spaces using scanf [duplicate]

This question already has answers here:
How do you allow spaces to be entered using scanf?
(11 answers)
Closed 6 years ago.
I'm with a little problem while doing an excercise to learn C.
The problem is: I need to read a string from the user, but if he types just a space, I need to print a space. That's okay in theory.
But, when I type the space while the program is running, it doesn't understand it as a string and it keeps waiting for me to type other things.
I'm using the scanf("%[^\n]", string_name_here);
I appreciate your help, and have a nice day! o/
And sorry for my bad english, I hope you can understand this :)
Using char *fgets(char *str, int n, FILE *stream) will make your day.
According to man :
fgets() reads in at most one less than size characters from stream and
stores them into the buffer pointed to by s. Reading stops after an
EOF or a newline. If a newline is read, it is stored into the buffer.
A terminating null byte ('\0') is stored after the last character in
the buffer.
Example program
#include <stdio.h>
#define MAXSTR 21
int main(void)
{
char my_str[MAXSTR] = {'\0'};
fgets(my_str, MAXSTR, stdin);
return 0;
}
Input :
Claudio Cortese
Output :
Claudio Cortese

How to only copy over the first n number of chars from a scanf in C [duplicate]

This question already has answers here:
Max string length using scanf -> ANSI C
(5 answers)
Closed 8 years ago.
Sorry I'm sure how to word my question properly.
I'm working on an assignment and I'd like to add a feature to the error checking.
I have a scanf that asks for a word (which is only 5 characters long max).
If the user inputs a string that is 6 characters long like candle, it will only copy over candl.
Here is my take on it. Though I'm sure this isn't the most efficient way to do it.
printf("\n\tEnter word: ");
scanf("%s", input);
if (strlen(input) > MAX_LETTERS){
int i = 0;
while (i < MAX_LETTERS){
word[i] = input[i];
i++;
}
printf("the word is %s", word);
}
Basically checks to see if the input is greater than the max amount of letters allowed. And then if it is, it loops and then only copies over the max amount of letters. Any help would be very much appreciated. Thank you in advance!
scanf("%ns", input);
Where n= number of characters to be read.If in your case it is 5 then have
scanf("%5s", input);
To avoid buffer overflow using scanf() you can specify the number of characters to be read as shown above. Assuming this is what you wanted .
I would always suggest using fgets() while reading strings
fgets(input,sizeof(input),stdin);
You can specify how many characters you want in the second parameter.
PS: fgets() comes with a newline character

Resources