stdin

Checking Standard Input in C#

随声附和 提交于 2019-12-02 23:09:31
I'm writing a small command line utility whose purpose is to parse the output of another utility. I want it to be invokable in two ways: c:\> myutility filewithoutput.txt Or, c:\> otherutility -args | myutility So, basically, standard in or a file argument. My first attempt at this looked like this: TextReader reader; if (args.Length > 1) { reader = new StreamReader(new FileStream(args[1], FileMode.Open)); } else { reader = Console.In; } Process(reader); The file argument works fine, and piping the output from the utility to my utility works fine, but if you just invoke it normally (no

ffmpeg: which file formats support stdin usage?

余生颓废 提交于 2019-12-02 22:37:11
I know ffmpeg is able to read data from stdin rather than reading from disk using ffmpeg -i - . Is this supported for all file formats? If it is not, is there a list which file formats are supported? Serge You need to run ffmpeg -protocols to determine if the pipe protocol (the read and write from stdin and stdout) supported in your version of ffmpeg and then ffmpeg -formats to see the list of supported formats. In the excerpt below you will see the note on output pipe that it must be seekable for some protocols. For input protocols it has no such restriction. From man ffmpeg-protocols :

How do I iterate over all lines of files passed on the command line?

旧城冷巷雨未停 提交于 2019-12-02 22:30:32
I usually do this in Perl: whatever.pl while(<>) { #do whatever; } then cat foo.txt | whatever.pl Now, I want to do this in Python. I tried sys.stdin but I have no idea how to do as I have done in Perl. How can I read the input? Try this: import fileinput for line in fileinput.input(): process(line) import sys def main(): for line in sys.stdin: print line if __name__=='__main__': sys.exit(main()) Something like this: import sys for line in sys.stdin: # whatever import sys for line in sys.stdin: # do stuff w/line I hate to beat a dead horse, but may I suggest using a pure function? import sys

read() stdin in c?

岁酱吖の 提交于 2019-12-02 22:17:12
问题 I have a quick little question involving the read() command. I am really rusty in C, and unfortunately, my current assignment has me programming in C. We need to read stdin using the read() and not fgets and the like. So I have a simple while loop: int n, i; char buffer[257], *input; while((n=read(fileno(stdin), buffer, 256)) > 0) { buffer[n] ='\0'; if(buffer[n] = '\n') break; write(input, buffer, strlen(buffer)); } So I am not sure how to make this loop stop at the press of enter (end of

Read from stdin write to stdout in C

老子叫甜甜 提交于 2019-12-02 21:27:38
I am trying to write a cat clone to exercise C, I have this code: #include <stdio.h> #define BLOCK_SIZE 512 int main(int argc, const char *argv[]) { if (argc == 1) { // copy stdin to stdout char buffer[BLOCK_SIZE]; while(!feof(stdin)) { size_t bytes = fread(buffer, BLOCK_SIZE, sizeof(char),stdin); fwrite(buffer, bytes, sizeof(char),stdout); } } else printf("Not implemented.\n"); return 0; } I tried echo "1..2..3.." | ./cat and ./cat < garbage.txt but I don't see any output on terminal. What I am doing wrong here? Edit: According to comments and answers, I ended up doing this: void copy

Golang read from pipe reads tons of data

强颜欢笑 提交于 2019-12-02 20:55:33
I'm trying to read an archive that's being tarred, streaming, to stdin, but I'm somehow reading far more data in the pipe than tar is sending. I run my command like this: tar -cf - somefolder | ./my-go-binary The source code is like this: package main import ( "bufio" "io" "log" "os" ) // Read from standard input func main() { reader := bufio.NewReader(os.Stdin) // Read all data from stdin, processing subsequent reads as chunks. parts := 0 for { parts++ data := make([]byte, 4<<20) // Read 4MB at a time _, err := reader.Read(data) if err == io.EOF { break } else if err != nil { log.Fatalf(

How to read non-ASCII characters from CLI standard input

为君一笑 提交于 2019-12-02 20:44:57
If I type å in CMD, fgets stop waiting for more input and the loop runs until I press ctrl-c . If I type a "normal" characters like a-z0-9!?() it works as expected. I run the code in CMD under Windows 7 with UTF-8 as charset ( chcp 65001 ), the file is saved as UTF-8 without bom. I use PHP 5.3.5 (cli). <?php echo "ÅÄÖåäö work here.\n"; while(1) { echo '> '. fgets(STDIN); } ?> If I change charset to chcp 1252 the loop doesn't break when I type å and it print "> å" but the "ÅÄÖåäö work here" become "ÅÄÖåäö work here!". And I know that I can change the file to ANSI, but then I can't use

How can I read piped input in Perl on Windows?

牧云@^-^@ 提交于 2019-12-02 20:38:37
I am trying to create something in Perl that is basically like the Unix tee command. I'm trying to read each line of STDIN , run a substitution on it, and print it. (And eventually, also print it to a file.) This works if I'm using console input, but if I try to pipe input to the command it doesn't do anything. Here's a simple example: print "about to loop\n"; while(<STDIN>) { s/2010/2009/; print; } print "done!\n"; I try to pipe the dir command to it like this: C:\perltest>dir | mytee.pl about to loop done! Why is it not seeing the piped input? (I'm using Perl 5.10.0 on WinXP, if that is

How to read from stdin or from a file if no data is piped in Python?

若如初见. 提交于 2019-12-02 19:15:39
I have a CLI script and want it to read data from a file. It should be able to read it in two ways : cat data.txt | ./my_script.py ./my_script.py data.txt —a bit like grep , for example. What I know: sys.argv and optparse let me read any args and options easily. sys.stdin let me read data piped in fileinput make the full process automatic Unfortunately: using fileinput uses stdin and any args as input. So I can't use options that are not filenames as it tries to open them. sys.stdin.readlines() works fine, but if I don't pipe any data, it hangs until I enter Ctrl + D I don't know how to

gets into char *str without malloc, no segfault?

♀尐吖头ヾ 提交于 2019-12-02 18:19:10
问题 I've just compiled this C program using Cygwin's gcc: #include <stdio.h> void main (){ char *str; gets(str); printf("%s",str); } Setting aside gets is deprecated gone, this is supposed to break since I'm not allocating any memory for str, but it works even with very long inputs. If, for example, I set char str[16] it breaks after exceeding the allocated length by just a few characters. How come I'm not getting a segmentation fault? 回答1: Access memory region pointed to by uninitialized pointer