Sponsored Content
Top Forums Programming Why does fflush(stdin) fail to work ? Post 303024405 by Don Cragun on Monday 8th of October 2018 05:25:43 AM
Old 10-08-2018
A call to fflush() flushes data held in an output stream buffer to the underlying file; it doesn't flush input streams. The return value from scanf() has told you that you don't have a floating point number at the start of the stream, but there is still unmatched data sitting in the buffer. If there wasn't any unmatched data, it would have reported EOF. Another call to scanf() with a different format might successfully match a hexadecimal input value, a character value, or a string value. If you replace the fflush(stdin); with another scanf() to skip over a string value (i.e. scanf("%*s");), you might get what you want... Or, you might not. You haven' t given a very clear description of what sort of input might be entered by your input source.

Note, however, that an EOF or an I/O error condition on the input stream will still leave you with an infinite loop. And, if you're dealing with humans typing input, you need to perform much better error handling.
 

10 More Discussions You Might Find Interesting

1. Programming

stdin

hi, how does a program know whether some data are available from stdin? I would like to make a program which could read its data from stdin and _if_there_is_nothing_at_stdin_ from a file which name is given as an argument. If there is nothing in stdin and no filename is given as argument,... (2 Replies)
Discussion started by: marquis
2 Replies

2. Shell Programming and Scripting

redirection stdin

hello all, I need to create a password change utility for a database. I need to gather at the command line the username, password and database sid. I have the program currently doing this. What I would like to do is not have the new password appear on the screen when I do my read command.... (2 Replies)
Discussion started by: whited05
2 Replies

3. UNIX for Dummies Questions & Answers

Script doesn't work, but commands inside work

Howdie everyone... I have a shell script RemoveFiles.sh Inside this file, it only has two commands as below: rm -f ../../reportToday/temp/* rm -f ../../report/* My problem is that when i execute this script, nothing happened. Files remained unremoved. I don't see any error message as it... (2 Replies)
Discussion started by: cheongww
2 Replies

4. Shell Programming and Scripting

redirect STDIN

can you redirect STDIN with command arguments? I have tried this approach: # ./script -option <argument1> <argument2> 0<$2 # $2: ambiguous redirect Is this possible? (4 Replies)
Discussion started by: prkfriryce
4 Replies

5. Programming

How to write to stdin of another program (program A -> [stdin]program B)

Hi, Program A: uses pipe() I am able to read the stdout of PROGAM B (stdout got through system() command) into PROGRAM A using: * child -> dup2(fd, STDOUT_FILENO); -> execl("/path/PROGRAM B", "PROGRAM B", NULL); * parent -> char line; -> read(fd, line, 100); Question: ---------... (1 Reply)
Discussion started by: vvaidyan
1 Replies

6. UNIX for Dummies Questions & Answers

How to write to stdin of another program (program A -> [stdin]program B)

Hi, Program A: uses pipe() I am able to read the stdout of PROGAM B (stdout got through system() command) into PROGRAM A using: * child -> dup2(fd, STDOUT_FILENO); -> execl("/path/PROGRAM B", "PROGRAM B", NULL); * parent -> char line; -> read(fd, line, 100); Question: ---------... (3 Replies)
Discussion started by: vvaidyan
3 Replies

7. UNIX for Dummies Questions & Answers

fork and stdin

When a process fork(), the child share the same file descriptors as his father. Thus, they share the same stdin. Quick and dirty exemple below (sorry for the ugly gets() call) : #include <stdio.h> #include <unistd.h> int main() { char buf; if (fork()) { /*parent */ ... (1 Reply)
Discussion started by: milouz
1 Replies

8. UNIX for Dummies Questions & Answers

redirection stdin

Bonjour, Mon application en C sous linux tourne en redirigeant stdin vers un fichier. Exemple; $appli1 <file1. PB: Je voudrais temporairement redonner la main au user sur le clavier. Alors je pensais ajouter system("appli2"); dans appli1. Dans son main() , appli2() fait seulement un... (1 Reply)
Discussion started by: cypleen
1 Replies

9. Shell Programming and Scripting

tr command fail to work in script

Hi, I has the following command in the script. This command works fine if I execute on command prompt. If I run the script, this is not working as expected (deleting CR). tr -d "\015" < ${FilePath}/${FileName} > ${FilePath}/${File_Prefix}.csv I could not figure out whats... (6 Replies)
Discussion started by: kavuri
6 Replies

10. IP Networking

Discussion at work, would a router work pluging a cable in wan1 and lan1?

hi all. and sorry for the random question, but this sparkled a raging flame-war at work and i want more points of view situation a router, with linux of some sort, dhcp client requesting for ip in wan1 (as usual with wan ports) dhcp server listening in lan1, and assigning ip (as usual... (9 Replies)
Discussion started by: broli
9 Replies
SCANF(3S)																 SCANF(3S)

NAME
scanf, fscanf, sscanf - formatted input conversion SYNOPSIS
#include <stdio.h> scanf(format [ , pointer ] . . . ) char *format; fscanf(stream, format [ , pointer ] . . . ) FILE *stream; char *format; sscanf(s, format [ , pointer ] . . . ) char *s, *format; DESCRIPTION
Scanf reads from the standard input stream stdin. Fscanf reads from the named input stream. Sscanf reads from the character string s. Each function reads characters, interprets them according to a format, and stores the results in its arguments. Each expects as arguments a control string format, described below, and a set of pointer arguments indicating where the converted input should be stored. The control string usually contains conversion specifications, which are used to direct interpretation of input sequences. The control string may contain: 1. Blanks, tabs or newlines, which match optional white space in the input. 2. An ordinary character (not %) which must match the next character of the input stream. 3. Conversion specifications, consisting of the character %, an optional assignment suppressing character *, an optional numerical maximum field width, and a conversion character. A conversion specification directs the conversion of the next input field; the result is placed in the variable pointed to by the corre- sponding argument, unless assignment suppression was indicated by *. An input field is defined as a string of non-space characters; it extends to the next inappropriate character or until the field width, if specified, is exhausted. The conversion character indicates the interpretation of the input field; the corresponding pointer argument must usually be of a restricted type. The following conversion characters are legal: % a single `%' is expected in the input at this point; no assignment is done. d a decimal integer is expected; the corresponding argument should be an integer pointer. o an octal integer is expected; the corresponding argument should be a integer pointer. x a hexadecimal integer is expected; the corresponding argument should be an integer pointer. s a character string is expected; the corresponding argument should be a character pointer pointing to an array of characters large enough to accept the string and a terminating `', which will be added. The input field is terminated by a space character or a new- line. c a character is expected; the corresponding argument should be a character pointer. The normal skip over space characters is suppressed in this case; to read the next non-space character, try `%1s'. If a field width is given, the corresponding argument should refer to a character array, and the indicated number of characters is read. e a floating point number is expected; the next field is converted accordingly and stored through the corresponding argument, which f should be a pointer to a float. The input format for floating point numbers is an optionally signed string of digits possibly contain- ing a decimal point, followed by an optional exponent field consisting of an E or e followed by an optionally signed integer. [ indicates a string not to be delimited by space characters. The left bracket is followed by a set of characters and a right bracket; the characters between the brackets define a set of characters making up the string. If the first character is not circumflex (^), the input field is all characters until the first character not in the set between the brackets; if the first character after the left bracket is ^, the input field is all characters until the first character which is in the remaining set of characters between the brackets. The corresponding argument must point to a character array. The conversion characters d, o and x may be capitalized or preceded by l to indicate that a pointer to long rather than to int is in the argument list. Similarly, the conversion characters e or f may be capitalized or preceded by l to indicate a pointer to double rather than to float. The conversion characters d, o and x may be preceded by h to indicate a pointer to short rather than to int. The scanf functions return the number of successfully matched and assigned input items. This can be used to decide how many input items were found. The constant EOF is returned upon end of input; note that this is different from 0, which means that no conversion was done; if conversion was intended, it was frustrated by an inappropriate character in the input. For example, the call int i; float x; char name[50]; scanf("%d%f%s", &i, &x, name); with the input line 25 54.32E-1 thompson will assign to i the value 25, x the value 5.432, and name will contain `thompson'. Or, int i; float x; char name[50]; scanf("%2d%f%*d%[1234567890]", &i, &x, name); with input 56789 0123 56a72 will assign 56 to i, 789.0 to x, skip `0123', and place the string `56' in name. The next call to getchar will return `a'. SEE ALSO
atof(3), getc(3S), printf(3S) DIAGNOSTICS
The scanf functions return EOF on end of input, and a short count for missing or illegal data items. BUGS
The success of literal matches and suppressed assignments is not directly determinable. 7th Edition May 15, 1985 SCANF(3S)
All times are GMT -4. The time now is 02:31 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy