C-Troubleshoot with gets() - c

When I give the first input, an extra 0 appears before gets() works. But if I remove gets(), then there is no problem. printf() can't be used because it breaks on blank space. Please give any alternative solution or what should I do?
#include <cstdio>
#include <iostream>
#include <stdlib.h>
using namespace std;
int main()
{
long long a,i,t,count;
int op;
char s[10000];
scanf("%lld",&t);
for(i=1;i<=t;i++)
{
gets(s);
a=atoll(&s[7]);
printf("%lld",a);
}
return 0;
}

The scanf() leaves the end-of-line character of the first line in the input stream which is then consumed by gets(). This is a common beginner's error often discussed here.
Recommendations:
Do not mix scanf() routines with gets() routines.
Except for short test programs do not use gets() (instead use fgets()) because with gets() buffer overflows may occur.

You can try adding the '\n' character when you are reading with scanf:
scanf("%lld\n",&t);
for(i=1;i<=t;i++)
{
gets(s);
a=atoll(&s[7]);
printf("%lld",a);
}

Why not:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
long long i;
long long t;
char s[10000];
if (fgets(s, sizeof(s), stdin) == 0)
return 0;
t = atoll(s);
for (i = 1; i <= t; i++)
{
if (fgets(s, sizeof(s), stdin) == 0)
break;
a = atoll(&s[7]);
printf("%lld\n", a);
}
return 0;
}
Amongst other merits, it doesn't:
print stray zeroes,
include C++ code in a purportedly C program,
contain any stray (unused) variables,
use the dangerous gets() function.
It is fair to note a couple of deficiencies:
It would produce bogus output if a data line was not at least 8 characters long; it should check strlen(s) before calling atoll(&s[7])
We'll assume that 10K is longer than any single line it will be given to read so truncated lines won't be a problem, though JSON data sometimes seems to get encoded in files without any newlines and can be humongously long (Firefox bookmark lists or backups, for example, don't even contain a single newline).

I'm sure what you're trying to do here, or what the problem is. But ...
As Greg Hewgill correctly said: NEVER use "gets()". It's a buffer overflow waiting to happen.
You CAN use "fgets()" - and it could easily solve the problem.
While you're at it, why the "scanf()", followed by "gets()", followed by "atoll()"? Can any of these inputs be merged? Or made more consistent?
Where are you checking for a valid conversion from "atoll()"? Why not just use "sscanf()" (and check the return value)?

Related

Getting multiple strings as inputs

The Question is to take three names as input and check whether the 4th input is the first letter of one of those three names.
*It needs to be only of alpha type and no other.
Sample Input:
#ON SNOW
ARYA STARK
HODOR
#
Output:
NO
My code:
#include <stdio.h>
#include <ctype.h>
int main(){
char s1[100],s2[100],s3[100];
char ch[1];
scanf("%[^\n]s",s1);
scanf("%[^\n]s",s2);
scanf("%[^\n]s",s3);
scanf("%s",ch);
if(isalpha(ch[0]) && (s1[0]==ch[0] || s2[0]==ch[0] || s3[0]==ch[0]))
printf("yes");
else
printf("no");
}
As I figured out that gets() no longer works, I tried scanf("%s",s1);.
But since it does not store the calude after the whitespace " ", I tried this scanf("%[^\n]s",s1);. But the scanning stops at the first name itself.
John Player
NO
What is the way to store multiple strings in a char array without using loops and only using branching??
Now before getting to the specified problem lets see what the actual problem is:
Problem with scanf()
The problem with scanf() is that it is really bad at managing overflows. And in case of chars or char sequences the newline character is read on the press of enter key in the subsequent scanning. There are numerous instances on SO that concern this problem. scanf() leaves the new line char in buffer? and Problems with scanf
Problem with gets()
The biggest drawback is that here you need to know the size of input before-hand. If you know your input extremely well, you may use it (Still I won't recommend). Why is the gets function so dangerous that it should not be used?
Problem with fgets()
There are two really common problems with fgets()
The syntax of fgets() is:
char *fgets(char *restrict s, int n, FILE *restrict stream);`
and generally used like
fgets(char_array,100,stdin)
First problem arises when the input is bigger than the integer n provided in the fgets() second parameter. When the input in the buffer is bigger than n it will strip off 1st n chars and allocate it the char pointer which might be a char array. But what about remaining chars? They still are there in the input buffer and will be allocated to next fgets(). Messing things up.
Second problem is that every time a new line feed is allocated to the end of char sequence when the input is smaller than the int n-1.
But if we think for a while the problems with fgets() can be tackled with a simple trick.
Just check for the last character in the char sequence which has been recently allocated. If it is new line, replace it by NULL. Else we know that the input was more than that int provided inside fgets(). So all we have to do is eat up the remaining chars in the input buffer.
Here is an example:
char str1[5];
char str2[5];
fgets(str1,5,stdin);
if(strlen(str1)>0){//to avoid Undefined Behavior in case of null byte input
if(str1[strlen(str1)-1]=='\n'){
str1[strlen(str1)-1]='\0';
}
else{
while((getchar())!='\n');//eating the remaing chars in the buffer
}
}
fgets(str2,5,stdin);
if(strlen(str2)>0){
if(str2[strlen(str2)-1]=='\n'){
str2[strlen(str2)-1]='\0';
}
else{
while((getchar())!='\n');
}
}
printf("\n1.%s\n2.%s",str1,str2);
You could even convert strings you got using fgets to float and integers using things like strol or sscanf, but beware they may not show independent behavior.
Now coming back to the solution to your problem:
#include <stdio.h>
#include <string.h>
int main(){
char s1[100],s2[100],s3[100];
char ch[2];//make ch atleast 2 char wide
fgets(s1,100,stdin);
if(strlen(s1)>0){
if(s1[strlen(s1)-1]=='\n'){
s1[strlen(s1)-1]='\0';
}
else{
while((getchar())!='\n');
}
}
fgets(s2,100,stdin);
if(strlen(s2)>0){
if(s2[strlen(s2)-1]=='\n'){
s2[strlen(s2)-1]='\0';
}
else{
while((getchar())!='\n');
}
}
fgets(s3,100,stdin);
if(strlen(s3)>0){
if(s3[strlen(s3)-1]=='\n'){
s3[strlen(s3)-1]='\0';
}
else{
while((getchar())!='\n');
}
}
fgets(ch,2,stdin);
if(strlen(ch)>0){
if(ch[strlen(ch)-1]=='\n'){
ch[strlen(ch)-1]='\0';
}
else{
while((getchar())!='\n');
}
}
if(isalpha(ch[0]) && (s1[0]==ch[0] || s2[0]==ch[0] || s3[0]==ch[0]))
printf("yes");
else
printf("no");
return 0;
}
Try fgets.
fgets(s1,100,stdin);
fgets(s2,100,stdin);
fgets(s3,100,stdin);
#include <stdio.h>
int main(){
char s1[100],s2[100],s3[100];
char ch[1];
fgets(s1,100,stdin);// like gets but limited by length
fgets(s2,100,stdin);
fgets(s3,100,stdin);
scanf("%s",ch);
if( (s1[0]>='a' && s1[0]<='z') || (s1[0]>='A' && s1[0]<='Z') ){//ch-alpha?
if(s1[0] == ch[0]){ printf("yes"); }//s1[0] == ch[0] ??
else{ printf("no"); }
}
else
printf("no"); return 0; }

fgets reading more char than it should

I'm developing a code where the user will type several paragraphs and it will stop reading when the user begin a paragraph with "END". The code will manipulate the string by counting each letter and showing a graph and blah blah blah, but this is irrelevant to the question.
The thing is: which paragraph must have no more than 1000 characters.
A smaller version of the code is the following (considering I just want to storage 5-char-string - even though I'll expand that).
#include <stdio.h>
#include <string.h>
int main()
{
char paragraph[5];
for ( ; ; )
{
fgets(paragraph, 5, stdin);
if (paragraph[0]=='E' && paragraph[1]=='N' && paragraph[2]=='D')
{ return 0; }
printf("%s", paragraph);
}
return 0;
My problem is: if I type more than 5 characters, the printf function still prints more than 5 characters, I don't know why. I've already checked everything I could possible check.
Help a beginner like me, please.
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.
So when entering more than 4 characters (newline included) only 4 is read and the rest stays in the buffer ready to be read next fgets.
Your printf will not print any newline in this case and will be called multiple times, making it look like printing more than 4 characters.
As suggested in comments, try printf("[%s]", paragraph); to see the individual printf calls.
You should use strstr in string.h because it's cleaner.
if (strstr(paragraph, "END"))
return 0;
instead of
if (paragraph[0]=='E' && paragraph[1]=='N' && paragraph[2]=='D')
return 0;
Try modifying your code in the following way and you'll immediately see what actually happens with the fgets() function when you enter more characters than the size of your buffer. It doesn't read from the keyboard, but from the stdinbuffer. These SO posts may also be interesting for you to read:(1), (2). Enjoy the demo and be sure to thoroughly read the man pages.
#include <stdio.h>
int main()
{
char paragraph[5];
for ( ; ; )
{
printf("Enter the string: \n\t");
if(fgets(paragraph, 5, stdin) != NULL)
printf("%s\n", paragraph);
if (paragraph[0]=='E' && paragraph[1]=='N' && paragraph[2]=='D')
return 0;
}
return 0;
}

Loop through user input with getchar

I have written a small script to detect the full value from the user input with the getchar() function in C. As getchar() only returns the first character i tried to loop through it... The code I have tried myself is:
#include <stdio.h>
int main()
{
char a = getchar();
int b = strlen(a);
for(i=0; i<b; i++) {
printf("%c", a[i]);
}
return 0;
}
But this code does not give me the full value of the user input.
You can do looping part this way
int c;
while((c = getchar()) != '\n' && c != EOF)
{
printf("%c", c);
}
getchar() returns int, not char. And it only returns one char per iteration. It returns, however EOF once input terminates.
You do not check for EOF (you actually cannot detect that instantly when getchar() to char).
a is a char, not an array, neither a string, you cannot apply strlen() to it.
strlen() returns size_t, which is unsigned.
Enable most warnings, your compiler wants to help you.
Sidenote: char can be signed or unsigned.
Read a C book! Your code is soo broken and you confused multiple basic concepts. - no offense!
For a starter, try this one:
#include <stdio.h>
int main(void)
{
int ch;
while ( 1 ) {
ch = getchar();
x: if ( ch == EOF ) // done if input terminated
break;
printf("%c", ch); // %c takes an int-argument!
}
return 0;
}
If you want to terminate on other strings, too, #include <string.h> and replace line x: by:
if ( ch == EOF || strchr("\n\r\33", ch) )
That will terminate if ch is one of the chars listed in the string literal (here: newline, return, ESCape). However, it will also match ther terminating '\0' (not sure if you can enter that anyway).
Storing that into an array is shown in good C books (at least you will learn how to do it yourself).
Point 1: In your code, a is not of array type. you cannot use array subscript operator on that.
Point 2: In your code, strlen(a); is wrong. strlen() calculates the length of a string, i.e, a null terminated char array. You need to pass a pointer to a string to strlen().
Point 3: getchar() does not loop for itself. You need to put getchar() inside a loop to keep on reading the input.
Point 4: getchar() retruns an int. You should change the variable type accordingly.
Point 5: The recommended signature of main() is int main(void).
Keeping the above points in mind,we can write a pesudo-code, which will look something like
#include <stdio.h>
#define MAX 10
int main(void) // nice signature. :-)
{
char arr[MAX] = {0}; //to store the input
int ret = 0;
for(int i=0; i<MAX; i++) //don't want to overrrun array
{
if ( (ret = getchar())!= EOF) //yes, getchar() returns int
{
arr[i] = ret;
printf("%c", arr[i]);
}
else
;//error handling
}
return 0;
}
See here LIVE DEMO
getchar() : get a char (one character) not a string like you want
use fgets() : get a string or gets()(Not recommended) or scanf() (Not recommended)
but first you need to allocate the size of the string : char S[50]
or use a malloc ( #include<stdlib.h> ) :
char *S;
S=(char*)malloc(50);
It looks like you want to read a line (your question mentions a "full value" but you don't explain what that means).
You might simply use fgets for that purpose, with the limitation that you have to provide a fixed size line buffer (and handle - or ignore - the case when a line is larger than the buffer). So you would code
char linebuf[80];
memset (linebuf, 0, sizeof(linbuf)); // clear the buffer
char* lp = fgets(linebuf, sizeof(linebuf), stdin);
if (!lp) {
// handle end-of-file or error
}
else if (!strchr(lp, '\n')) {
/// too short linebuf
}
If you are on a POSIX system (e.g. Linux or MacOSX), you could use getline (which dynamically allocates a buffer). If you want some line edition facility on Linux, consider also readline(3)
Avoid as a plague the obsolete gets
Once you have read a line into some buffer, you can parse it (e.g. using manual parsing, or sscanf -notice the useful %n conversion specification, and test the result count of sscanf-, or strtol(3) -notice that it can give you the ending pointer- etc...).

C echo user input

So I am a very beginner to C programming (I have used Ruby, Python and Haskell before) and I am having trouble getting the most simple thing to work in C (probably because of all the manual memory stuff). Anyway, what I am trying to do is (using simple constructs) make a script that just echoes what the user inputs to the console.
e.g. user inputs hi, console prints hi.
This is what I came up with.
Also, I haven't really mastered pointers, so none of that.
// echo C script
int echo();
int main() {
echo();
return 0;
}
int echo() {
char input[500];
while (1) {
if (scanf("%[^\n]", input) > 0) {
printf("%s\n", input);
}
input[0] = 0;
}
return 1;
}
I realize that there is a bunch of bad practices here, like setting a giant string array, but that is just for simplifying it.
Anyway, my problem is that it repeats the first input then the input freezes. As far as I can tell, it freezes during the while loop (1 is never returned).
Any help would be appreciated.
Oh, and using TCC as the compiler.
You don't need an array for echo
#include <stdio.h>
int main(void)
{
int c;
while((c = getchar()) != EOF) putchar(c);
return 0;
}
It's fine that you have such a large string allocated, as long as it's possible for users to input a string of that length. What I would use for input is fgets (read this for more information). Proper usage in your situation, given that you still would like to use the string of size 500, would be:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int echo(){
char input[500];
while(fgets(input, 500, STDIN)){ //read from STDIN (aka command-line)
printf("%s\n", input); //print out what user typed in
memset(input, 0, strlen(input)); //reset string to all 0's
}
return 1;
}
Note that changing the value of 500 to whatever smaller number (I would normally go with some power of 2 by convention, like 512, but it doesn't really matter) will limit the length of the user's input to that number. Also note that I didn't test my code but it should work.
scanf("%[^\n]", input
Should be:
scanf("%s",input)
Then after your if you should do:
memset(input,0,500);
There are many ways of accomplishing this task however the easiest would be to read from stdin one byte at a time and output that byte to stdout as you process each byte.
Snippet:
#include <stdio.h>
int main( void ) {
// Iterates until EOF is sent.
for ( int byte = getchar(); byte != EOF; byte = getchar() ) {
// Outputs to stdout the byte.
putchar( byte );
}
return 0;
}
Remark:
You must store the byte that you are reading through stdin in an integer. This is because you are not guaranteed that char is signed or unsigned, there are in fact 3 char types in C (char, signed char and unsigned char). Include the limits library to determine whether a char is signed or not in your environment.
You must compile using the C99 standards, otherwise move the declaration of byte outside of the for loop.

Read arbitrarily sized string from stdin? [duplicate]

This question already has answers here:
How to read a line from the console in C?
(14 answers)
Closed 5 years ago.
I want to read a string from the console.
Using scanf or fgets however, it seems to me that it's only possible to read a string of a fixed maximum size. Even worse, there seems to be no way of checking how many characters were entered in case the user enters too much (in that case I could simply realloc the array in order for the string to fit into the array).
I read that I'm supposed to read one character at a time in the answer to this question, however I don't know how to read one character at a time without having the user press enter after each character.
How can I do it?
The GCC documentation says that:
Standard C has functions to do this, but they aren't very safe: null characters and even (for gets) long lines can confuse them. So the GNU library provides the nonstandard getline function that makes it easy to read lines reliably.
and that
[getline] is a GNU extension, but it is the recommended way to read lines from a stream. The alternative standard functions are unreliable.
So if you're using GCC, I'd say you should use getline. If you're not using GCC, you should see if your compiler offers a similar feature. If it doesn't — or if you really prefer to use something standard — then you need to read one character at a time.
I don't know how to read one character at a time without having the user press enter after each character.
Yes, you do. The user enters a sequence of characters, and then presses Enter. Your first fgetc call will block until the user presses Enter, but after that, subsequent fgetc calls will return immediately, up until you read the newline. (Then it will block again, until the user presses Enter again.) Reading "one character at a time" doesn't mean that you have to read each character before the user types the next one; it just means that, once the user is done typing a line, you read that line one character at a time.
Try running this..
#include <stdio.h>
int main(){
char next;
while((next=getchar())!=EOF)
printf("%c\n",next);
}
then check out the man page for getchar() to see what's really at hand.
char c = NULL;
while (c != 0x0D)
{
scanf("%c", &c);
// do stuffs with c
}
You can use fgets() in a loop and realloc if the last character is not a \n
/* UNTESTED: MAY HAVE OFF-BY-ONE ERRORS */
char *buffer;
size_t bufsiz = 100;
size_t buflen = 100;
size_t bufcur = 0;
buffer = malloc(bufsiz);
for (;;) {
fgets(buffer + bufcur, bufsiz, stdin);
buflen = bufcur + strlen(buffer + bufcur);
if (buffer[buflen - 1] == '\n') break;
tmp = realloc(buffer, bufsiz * 2);
if (tmp == NULL) /* deal with error */;
buffer = tmp;
bufcur = buflen - 1;
bufsiz *= 2;
}
/* use buffer (and bufsiz and buflen) */
free(buffer);
The accepted answer should note that getchar() returns an int. The char data type is not big enough to hold EOF.
We could read a predetermined amount of text and then discard the rest of the input. That approach has more than its share of critics (how dare we presume to know what to discard). The other option is to use getline or write a custom function. I thought I'd try the latter.
This does not prevent users from filling up memory with cat large_file.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#define MAX 50
#define JUSTDO(a) if (!(a)) {perror(#a);exit(1);}
/** char *get_line FILE *f
* reads an arbitrarily long line of text or until EOF
* caller must free the pointer returned after use
*/
char *get_line (FILE *f) {
int len=MAX;
char buf[MAX],*e=NULL,*ret;JUSTDO (ret=calloc(MAX,1));
while (fgets (buf,MAX,f)) {
if (len-strlen(ret)<MAX) JUSTDO (ret=realloc(ret,len*=2));
strcat (ret,buf) ;if ((e=strrchr (ret,'\n'))) break;
} if (e) *e='\0';
return ret;
}
/* main */
int main (void) {
char *s;
printf ("enter a line of text:\n");
printf ("line was '%s'\n",s=get_line(stdin)) ;free (s);
return 0;
}

Resources