stdin

How to end scanf by entering only one EOF

这一生的挚爱 提交于 2019-12-02 06:23:23
I am encoutering this problem. I am using while loop to scan string of numbers and need to end scanning and start proceeding the rest of my program. I just can't figure out, how to flush the stdin or do anything to not press Ctrl+D twice. I just need to send EOF only once to tell my loop to end. while (! feof (stdin)) {status=scanf ("%d", &array[i]); if ( (status != 1 && status != EOF) ) { printf("\nWrong input.\n"); return 1;} i++;} Edit: it's bug 1190 on glibc , it was apparently done purposefully for System V compatibility (and Solaris behaves in the same way, FreeBSD and NetBSD behave as

fflush() function not working with stdin

▼魔方 西西 提交于 2019-12-02 06:01:05
I'm sorry for this silly question. I have C program to prompt user to enter age and name and then print the age and name to the screen. This is my exercise that I read from book. This the program: #include <stdio.h> int main (void) { int age; char name[20]; puts("Enter your age:"); scanf("%d",&age); fflush(stdin); puts("Enter your name:"); scanf("%s",name); printf("Your age is %d\n",age); printf("Your name is %s\n",name); return 0; } When I enter extra characters to the first scanf() the program terminates and assign the extra characters to the next scanf() And then I changed the code, and add

Is there a way to use locked standard input and output in a constructor to live as long as the struct you're constructing?

余生长醉 提交于 2019-12-02 05:41:45
I'm building a PromptSet that can ask a series of questions in a row. For testing reasons, it allows you to pass a reader and writer instead of using stdin & stdout directly. Because stdin and stdout are the common use case, I would like to create a default "constructor" that allows the user to produce a PromptSet<StdinLock, StdoutLock> without needing any parameters. Here's the code so far: use std::io::{self, BufRead, StdinLock, StdoutLock, Write}; pub struct PromptSet<R, W> where R: BufRead, W: Write, { pub reader: R, pub writer: W, } impl<R, W> PromptSet<R, W> where R: BufRead, W: Write, {

Clearing out stdin in C when it may or may not be empty

旧巷老猫 提交于 2019-12-02 05:33:27
问题 I am a programming student looking for a way to get rid of characters that may be hanging around in stdin. I have tried a technique that has been given here in various forms, where you do something like this: void clearStdIn(void) { char c; while((c = getchar()) != '\n' && c != EOF) /* discard */ ; } The problem seems to be that if nothing is in stdin to begin with, this function sits around waiting for the user to hit enter before control flow can move on. What should I do? 回答1: Flushing an

Scanln in Golang doesn't accept whitespace

倾然丶 夕夏残阳落幕 提交于 2019-12-02 05:23:07
问题 How can I use Scanln that accepts whitespace as input? 回答1: You can't use the fmt package's Scanln() and similar functions for what you want to do, because quoting from fmt package doc: Input processed by verbs is implicitly space-delimited: the implementation of every verb except %c starts by discarding leading spaces from the remaining input, and the %s verb (and %v reading into a string) stops consuming input at the first space or newline character. The fmt package intentionally filters

Scanln in Golang doesn't accept whitespace

…衆ロ難τιáo~ 提交于 2019-12-02 04:04:13
How can I use Scanln that accepts whitespace as input? You can't use the fmt package's Scanln() and similar functions for what you want to do, because quoting from fmt package doc: Input processed by verbs is implicitly space-delimited: the implementation of every verb except %c starts by discarding leading spaces from the remaining input, and the %s verb (and %v reading into a string) stops consuming input at the first space or newline character. The fmt package intentionally filters out whitespaces, this is how it is implemented. Instead use bufio.Scanner to read lines that might contain

C - Reading from stdin as characters are typed

我怕爱的太早我们不能终老 提交于 2019-12-02 03:48:35
How do I fill an 80-character buffer with characters as they are being entered or until the carriage return key is pressed, or the buffer is full, whichever occurs first. I've looked into a lot of different ways, but enter has to be pressed then the input char* gets cut off at 80.. Thanks. If you really want the characters "as they are entered", you cannot use C io. You have to do it the unix way. (or windows way) #include <stdio.h> #include <unistd.h> #include <termios.h> int main() { char r[81]; int i; struct termios old,new; char c; tcgetattr(0,&old); new = old; new.c_lflag&=~ICANON;

Why inside a loop, scanf() with %d does not wait for the user input in case it received an invalid input previously?

无人久伴 提交于 2019-12-02 03:09:51
问题 I am using what scanf() returns when it gets what is expects or when it doesn't. What happens is it gets stuck in the while()` loop. To my knowledge test = scanf("%d", &testNum); returns a 1 if it receives a number and a 0 if not. My code: #include<stdio.h> int main(void) { while (1) { int testNum = 0; int test; printf("enter input"); test = scanf("%d", &testNum); printf("%d", test); if (test == 0) { printf("please enter a number"); testNum = 0; } else { printf("%d", testNum); } } return(0);

PowerShell wrapper to direct piped input to Python script

坚强是说给别人听的谎言 提交于 2019-12-02 03:09:20
I'm trying to write a little tool that will let me pipe command output to the clipboard. I've read through multiple answers on Stack Overflow, but they didn't work for me, because they didn't include piping, or because they didn't use a function, or they just threw errors (or maybe I just messed up). I threw up my hands with PowerShell and decided to go with Python. I created a Python script called copyToClipboard.py : import sys from Tkinter import Tk if sys.stdin.isatty() and len(sys.argv) == 1: #We're checking for input on stdin and first argument sys.exit() tk = Tk() tk.withdraw() tk

C low-level standard-in to accept filename then printing file contents to stdout

瘦欲@ 提交于 2019-12-02 02:25:45
问题 I want to get a file name from a user via stdin, open the file with open() and assign it to a file descriptor, then print the contents of that file to stdout. This is my code, and it's not working properly. Problems: the printf("enter filename"); statement is never showing up it never opens the file; instead whatever the user inputs is printed to the screen and then the "no such file or directory" error message is printed and the program exits after the program exists i see "enter filename"