The program does not wait for user input using scanf ("% c", & yn);

This is the base code for the program I am writing to use C files. I am trying to determine if the output file exists and whether it exists. I want to ask the user if they want to overwrite it or not. It is for this reason that I first opened the outfilename file with fopen (outfilename, "r"); unlike fopen (outfilename, "w");

It discovers that the file does not exist, however, if it exists, it executes printf ("The output file already exists, overwrites (y / n):"); but completely ignores scanf ("% c", & yn); expression!

printf at the end of the program reads "yn = 0" if the file does not exist and simply "yn =" if it exists. Can anybody help me?

#include <stdio.h> #include <stdlib.h> #include <float.h> #include <string.h> int main(void) { FILE *inf; FILE *outf; char filename[21],outfilename[21]; char yn='0'; printf("Please enter an input filename: "); scanf("%s",&filename); printf("Please enter an output filename: "); scanf("%s",&outfilename); /* Open file for reading */ inf=fopen (filename,"r"); outf=fopen(outfilename,"r"); /*check that input file exists*/ if (inf!=NULL) { /*check that the output file doesn't already exist*/ if (outf==NULL){ fclose(outf); /*if it doesn't already exist create file by opening in "write" mode*/ outf=fopen(outfilename,"w"); } else { /*If the file does exist, give the option to overwrite or not*/ printf("Output file already exists, overwrite (y/n):"); scanf("%c",&yn); } } printf("\n yn=%c \n",yn); return 0; } 
+4
source share
5 answers
 printf("Please enter an output filename: "); scanf("%s",&outfilename); 

When you enter the second line and press the ENTER key, the line and character are placed in the input buffer, namely: the entered line and the newline character. The string is consumed by scanf , but the new string remains in the input buffer.

Further,

 scanf("%c",&yn); 

Your next scanf to read a character just reads / consumes a new line and therefore never waits for user input.

The solution is to consume an extra line of newline using:

 scanf(" %c", &yn); ^^^ <------------Note the space 

Or using getchar()

You can check my answer here for a detailed step-by-step explanation of the problem.

+20
source

Using

 scanf("%20s",&filename); 

and remember that stdin is a string buffer, and in Linux follows the tty discipline

You can use GNU readline or ncurses if you need more granular control.

+1
source

scanf("%s", ...) leaves \ n the end of the line at the input. This does not cause a problem for the following, since scanf ("% s", ...) starts with skipping whites. scanf("%c", ...) not and therefore you are reading \n .

BTW You probably encountered other problems: you put spaces in your file name ( %s does not read them), and if you enter names that are too long (% s has no input length restrictions).

One solution to the problem you complained about (but not the other) is to use scanf(" %c", ...) (see the space before %c ? scanf hard to use), which starts by skipping spaces.

0
source
 scanf("%s",&filename); 

also remove &

scanf.c: 13: warning: format '% s' expects type 'char', but argument 2 is of type 'char () [20u]'

0
source

The best way to deal with this problem that I have found is here .

It is recommended to use an alternative way to enter the handle and is very well explained.

I always use this function to get user input.

 char * read_line (char * buf, size_t length) { /**** Copyright de home.datacomm.ch/t_wolf/tw/c/getting_input.html#skip Read at most 'length'-1 characters from the file 'f' into 'buf' and zero-terminate this character sequence. If the line contains more characters, discard the rest. */ char *p; if ((p = fgets (buf, length, stdin))) { size_t last = strlen (buf) - 1; if (buf[last] == '\n') { /**** Discard the trailing newline */ buf[last] = '\0'; } else { /**** There no newline in the buffer, therefore there must be more characters on that line: discard them! */ fscanf (stdin, "%*[^\n]"); /**** And also discard the newline... */ (void) fgetc (stdin); } /* end if */ } /* end if */ return p; } /* end read_line */ 

Old answer

I fixed such problems with this rule:

 // first I get what I want. c = getchar(); // but after any user input I clear the input buffer // until the \n character: while (getchar() != '\n'); // this also discard any extra (unexpected) character. 

If you do this after any input, there should be no problem.

0
source

Source: https://habr.com/ru/post/1385748/


All Articles