stdin

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

廉价感情. 提交于 2019-12-02 01:34:05
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? Flushing an input stream (in a portable way) without blocking could be done like this: #include <stdlib.h> #include

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

Deadly 提交于 2019-12-02 00:21:56
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" printed before the prompt in terminal CODE: { printf("Enter the filename: "); read(STDIN_FILENO,

scanf not working on invalid input

ぃ、小莉子 提交于 2019-12-01 23:18:36
问题 On a character input in the first scanf() , the second one doesn't run. getchar() isn't working either for Try Again input. It skips to take input for Would you like to play again? (Y/N)? It seems that your_choice is supposed to take the character and check it afterward but the character is actually being taken by ch . What is causing it to work like this and how to resolve the issue. I've tried re-initializing the variables but doesn't work. #include <stdio.h> void choice(int); int main() {

How to read input from the terminal using /dev/stdin and read.csv()?

落爺英雄遲暮 提交于 2019-12-01 23:17:23
I'm using: R version 3.0.0 (2013-04-03) -- "Masked Marvel" Platform: x86_64-pc-linux-gnu (64-bit) I try to use read.csv to input a little CSV data snippet + header, directly from the terminal. I'm encountering a problem that may be related to R skips lines from /dev/stdin and read.csv, header on first line, skip second line but is different enough (the answers there don't explain what I see here) to warrant a separate question. R seems to skip the header line and treat the second (data) line as header: R> d <- read.csv(file='/dev/stdin', header=TRUE) a,b 1,2 3,4 # hit CTRL-D twice here to end

scanf not working on invalid input

こ雲淡風輕ζ 提交于 2019-12-01 23:15:08
On a character input in the first scanf() , the second one doesn't run. getchar() isn't working either for Try Again input. It skips to take input for Would you like to play again? (Y/N)? It seems that your_choice is supposed to take the character and check it afterward but the character is actually being taken by ch . What is causing it to work like this and how to resolve the issue. I've tried re-initializing the variables but doesn't work. #include <stdio.h> void choice(int); int main() { char ch; int random, your_choice; do { srand(time(NULL)); system("cls"); printf("** 0 is for Rock **\n"

How to background a process via proc_open and have access to STDIN?

半世苍凉 提交于 2019-12-01 22:16:21
I'm happily using proc_open to pipe data into another PHP process. something like this $spec = array ( 0 => array('pipe', 'r'), // I don't need output pipes ); $cmd = 'php -f another.php >out.log 2>err.log'; $process = proc_open( $cmd, $spec, $pipes ); fwrite( $pipes[0], 'hello world'); fclose( $pipes[0] ); proc_close($process); In the other PHP file I echo STDIN with: echo file_get_contents('php://stdin'); This works fine, but not when I background it. Simply by appending $cmd with & I get nothing from STDIN. I must be missing something fundamental. It also fails with fgets(STDIN) Any ideas

redirect a file contents to standard input in php

﹥>﹥吖頭↗ 提交于 2019-12-01 21:17:25
I have a file abc.txt with contents like: hello hi good bad ... .... Now, How to redirect the contents of the file line by line to a php script's standard input , so that when the php script is executed, it can collect the inputs by any of these commands: $f = fopen('php://stdin', 'r'); $line = fgets($f); or $f = fgets(STDIN); php yourscript.php < yourinputfile should do the trick. 来源: https://stackoverflow.com/questions/6321198/redirect-a-file-contents-to-standard-input-in-php

Scanf returns 0 without waiting for input

回眸只為那壹抹淺笑 提交于 2019-12-01 21:15:41
I have never programmed in C and today I have to write small code. Program is very easy - I want to add two integers. But when I'm trying to check if given input is a number and first scanf returns 0, the second one returns 0 too without waiting for input. Code: int main() { int a = 0; int b = 0; printf("Number a:\n"); if (scanf("%d", &a) != 1) { printf("Not a number. a=0!\n"); a = 0; } printf("Number b:\n"); if (scanf("%d", &b) != 1) { printf("Not a number. b=0!\n"); b = 0; } printf("%d\n", a+b); return 0; } That is because, once the first scanf() failed, it is probably because of matching

How to get the name of a file acting as stdin/stdout?

假如想象 提交于 2019-12-01 21:11:16
I'm having the following problem. I want to write a program in Fortran90 which I want to be able to call like this: ./program.x < main.in > main.out Additionally to "main.out" (whose name I can set when calling the program), secondary outputs have to be written and I wanted them to have a similar name to either "main.in" or "main.out" (they are not actually called "main"); however, when I use: INQUIRE(UNIT=5,NAME=sInputName) The content of sInputName becomes "Stdin" instead of the name of the file. Is there some way to obtain the name of files that are linked to stdin/stdout when the program

Read file without fopen() (C language)

会有一股神秘感。 提交于 2019-12-01 20:37:09
问题 I am working on a school project in which we have to do some operations (select, min, max) on a table saved in .txt file. The problem is that we can't use common functions such as fopen, fscanf, fclose. The program will be launched from command line like this: .\project.exe select parameters <table.txt Do you have some ideas how to get content of the .txt file to stdin without using fopen? Thanks. 回答1: You do not need to open the file - the operating environment will do it for you. When your