stdin

Using fseek with a file pointer that points to stdin

萝らか妹 提交于 2019-11-26 12:19:39
问题 Depending on command-line arguments, I\'m setting a file pointer to point either towards a specified file or stdin (for the purpose of piping). I then pass this pointer around to a number of different functions to read from the file. Here is the function for getting the file pointer: FILE *getFile(int argc, char *argv[]) { FILE *myFile = NULL; if (argc == 2) { myFile = fopen(argv[1], \"r\"); if (myFile == NULL) fprintf(stderr, \"File \\\"%s\\\" not found\\n\", argv[1]); } else myFile = stdin;

How do file descriptors work?

删除回忆录丶 提交于 2019-11-26 10:10:25
问题 Can someone tell me why this does not work? I\'m playing around with file descriptors, but feel a little lost. #!/bin/bash echo \"This\" echo \"is\" >&2 echo \"a\" >&3 echo \"test.\" >&4 The first three lines run fine, but the last two error out. Why? 回答1: File descriptors 0, 1 and 2 are for stdin, stdout and stderr respectively. File descriptors 3, 4, .. 9 are for additional files. In order to use them, you need to open them first. For example: exec 3<> /tmp/foo #open fd 3. echo "test" >&3

Why can&#39;t we read one character at a time from System.in?

☆樱花仙子☆ 提交于 2019-11-26 09:53:03
问题 The program below prints each character written on standard in, but only after a new-line has been written (at least on my system!). public class Test { public static void main(String[] args) throws java.io.IOException { int c; while ((c = System.in.read()) != -1) System.out.print((char) c); } } This prevents people from writing stuff like \"Press any key to continue\" and forces something like \"Press enter to continue.\" What is the underlying reason for this? Is it a limitation of Java? Is

Is there any way to peek at the stdin buffer?

☆樱花仙子☆ 提交于 2019-11-26 09:51:15
问题 We know that stdin is, by default, a buffered input; the proof of that is in usage of any of the mechanisms that \"leave data\" on stdin , such as scanf() : int main() { char c[10] = {\'\\0\'}; scanf(\"%9s\", c); printf(\"%s, and left is: %d\\n\", c, getchar()); return 0; } ./a.out hello hello, and left is 10 10 being newline of course... I\'ve always been curious, is there any way to \"peek\" at the stdin buffer without removing whatever may reside there? EDIT A better example might be:

launch an exe/process with stdin stdout and stderr?

 ̄綄美尐妖づ 提交于 2019-11-26 09:40:17
问题 With C++ how do i launch an exe/process with stdin stdout and stderr? I know how to do this in .NET and i remember using popen in the past but popen seems to allow stdin OR stdout not both and not all 3. I need this for windows but a linux solution is welcome as i\'ll need it for the same project in the future. 回答1: You shoud use CreateProcess from WinApi. It takes as argument an object of struct STARTUP_INFO type. You can set hStdin, hStdout, and hStderr fields of the object to redirect

Scanf/Printf double variable C

霸气de小男生 提交于 2019-11-26 09:39:03
问题 Let\'s say I have this following bit of code in C: double var; scanf(\"%lf\", &var); printf(\"%lf\", var); printf(\"%f\", var); It reads from stdin variable \'var\' and then prints twice in stdout \'var\'. I understand that\'s how you read a double variable from stdin, but my questions are: Why can you print a double with %lf? Why can you print a double with %f? Which one is better and correct to use? 回答1: For variable argument functions like printf and scanf , the arguments are promoted, for

PHP CLI: How to read a single character of input from the TTY (without waiting for the enter key)?

感情迁移 提交于 2019-11-26 09:30:50
问题 I want to read a single character at-a-time from the command line in PHP, however it seems as though there is some kind of input buffering from somewhere preventing this. Consider this code: #!/usr/bin/php <?php echo \"input# \"; while ($c = fread(STDIN, 1)) { echo \"Read from STDIN: \" . $c . \"\\ninput# \"; } ?> Typing in \"foo\" as the input (and pressing enter), the output I am getting is: input# foo Read from STDIN: f input# Read from STDIN: o input# Read from STDIN: o input# Read from

Does reading from stdin flush stdout?

最后都变了- 提交于 2019-11-26 09:03:54
问题 stdout is line-buffered when connected to a terminal, but I remember reading somewhere that reading (at least from stdin) will automatically flush stdout. All C implementations that I have used have done this, but I can\'t find it in the standard now. It does make sense that it works that way, otherwise code like this: printf(\"Type some input: \"); fgets(line, sizeof line, stdin); would need an extra fflush(stdout); So is stdout guaranteed to be flushed here? EDIT: As several replies have

PHP standard input?

孤者浪人 提交于 2019-11-26 08:56:53
问题 I know PHP is usually used for web development, where there is no standard input, but PHP claims to be usable as a general-purpose scripting language, if you do follow it\'s funky web-based conventions. I know that PHP prints to stdout (or whatever you want to call it) with print and echo , which is simple enough, but I\'m wondering how a PHP script might get input from stdin (specifically with fgetc() , but any input function is good), or is this even possible? 回答1: It is possible to read

How to read from stdin line by line in Node

∥☆過路亽.° 提交于 2019-11-26 08:47:38
问题 I\'m looking to process a text file with node using a command line call like: node app.js < input.txt Each line of the file needs to be processed individually, but once processed the input line can be forgotten. Using the on-data listener of the stdin, I get the input steam chunked by a byte size so I set this up. process.stdin.resume(); process.stdin.setEncoding(\'utf8\'); var lingeringLine = \"\"; process.stdin.on(\'data\', function(chunk) { lines = chunk.split(\"\\n\"); lines[0] =