stdin

Reading from stdin

笑着哭i 提交于 2019-11-27 20:04:28
What are the possible ways for reading user input using read() system call in Unix. How can we read from stdin byte by byte using read() ? You can do something like this to read 10 bytes: char buffer[10]; read(STDIN_FILENO, buffer, 10); remember read() doesn't add '\0' to terminate to make it string (just gives raw buffer). To read 1 byte at a time: char ch; while(read(STDIN_FILENO, &ch, 1) > 0) { //do stuff } and don't forget to #include <unistd.h> , STDIN_FILENO defined as macro in this file. There are three standard POSIX file descriptors, corresponding to the three standard streams, which

How to read the standard input into string variable until EOF in C?

佐手、 提交于 2019-11-27 19:55:13
I am getting "Bus Error" trying to read stdin into a char* variable. I just want to read whole stuff coming over stdin and put it first into a variable, then continue working on the variable. My Code is as follows: char* content; char* c; while( scanf( "%c", c)) { strcat( content, c); } fprintf( stdout, "Size: %d", strlen( content)); But somehow I always get "Bus error" returned by calling cat test.txt | myapp , where myapp is the compiled code above. My question is how do i read stdin until EOF into a variable? As you see in the code, I just want to print the size of input coming over stdin,

aysncio cannot read stdin on Windows

感情迁移 提交于 2019-11-27 19:18:41
问题 I'm trying to read stdin asynchronously on Windows 7 64-bit and Python 3.4.3 I tried this inspired by an SO answer: import asyncio import sys def reader(): print('Received:', sys.stdin.readline()) loop = asyncio.get_event_loop() task = loop.add_reader(sys.stdin.fileno(), reader) loop.run_forever() loop.close() However, it raises an OSError: [WInError 100381] An operation was attempted on something that is not a socket . Could a file-like object like stdin be wrapped in a class to give it the

node.js: readSync from stdin?

拟墨画扇 提交于 2019-11-27 18:41:29
Is it possible to synchronously read from stdin in node.js? Because I'm writing a brainfuck to JavaScript compiler in JavaScript (just for fun). Brainfuck supports a read operation which needs to be implemented synchronously. I tried this: const fs = require('fs'); var c = fs.readSync(0,1,null,'utf-8'); console.log('character: '+c+' ('+c.charCodeAt(0)+')'); But this only produces this output: fs:189 var r = binding.read(fd, buffer, offset, length, position); ^ Error: EAGAIN, Resource temporarily unavailable at Object.readSync (fs:189:19) at Object.<anonymous> (/home/.../stdin.js:3:12) at

How to create a pseudo-tty for reading output and writing to input

こ雲淡風輕ζ 提交于 2019-11-27 18:20:13
问题 I am using fork() and execvp() to spawn a process that must believe it is connected to an interactive terminal for it to function properly. Once spawned, I want to capture all the output from the process, as well as be able to send input to the process. I suspect psuedo-ttys may help here. Does anyone have a snippet on how to do this? 回答1: You want to call forkpty(). From the man page: #include <pty.h> /* for openpty and forkpty */ pid_t forkpty(int *amaster, char *name, struct termios *termp

How to pass the value of a variable to the stdin of a command?

回眸只為那壹抹淺笑 提交于 2019-11-27 18:14:21
I'm writing a shell script that should be somewhat secure i.e. does not pass secure data through parameters of commands and preferably does not use temporary files. How can I pass a variable to the stdin of a command? Or, if it's not possible, how to correctly use temporary files for such task? Something as simple as: echo "$blah" | my_cmd Passing a value on stdin is as simple as: your-command <<< "$your_variable" Always make sure you put quotes around variable expressions! Note that the ' echo "$var" | command operations mean that standard input is limited to the line(s) echoed. If you also

What's the fastest way to read from System.in in Java?

谁说胖子不能爱 提交于 2019-11-27 17:14:41
I am reading bunch of integers separated by space or newlines from the standard in using Scanner(System.in) . Is there any faster way of doing this in Java? Is there any faster way of doing this in Java? Yes. Scanner is fairly slow (at least according to my experience). If you don't need to validate the input, I suggest you just wrap the stream in a BufferedInputStream and use something like String.split / Integer.parseInt . A small comparison: Reading 17 megabytes (4233600 numbers) using this code Scanner scanner = new Scanner(System.in); while (scanner.hasNext()) sum += scanner.nextInt();

Read all text from stdin to a string

喜夏-厌秋 提交于 2019-11-27 17:14:04
问题 I'm writing a program in Node.js that (in some situations) wants to act as a simple filter: read everything from stdin (up to end of file), do some processing, write the result to stdout. How do you do the 'read everything from stdin' part? The closest solutions I've found so far, seem to work either for one line at a time from the console, or else only work when stdin is a file not a pipe. 回答1: My boiler-plate for this one is a lot like the solution described in a comment above -- offering

Using Python to run executable and fill in user input

拈花ヽ惹草 提交于 2019-11-27 16:48:38
问题 I'm trying to use Python to automate a process that involves calling a Fortran executable and submitting some user inputs. I've spent a few hours reading through similar questions and trying different things, but haven't had any luck. Here is a minimal example to show what I tried last #!/usr/bin/python import subprocess # Calling executable ps = subprocess.Popen('fortranExecutable',shell=True,stdin=subprocess.PIPE) ps.communicate('argument 1') ps.communicate('argument 2') However, when I try

Test if stdin has input for C++ (windows and/or linux)

喜夏-厌秋 提交于 2019-11-27 15:04:16
I basically want to test if stdin has input (like if you echo and pipe it). I have found solutions that work, but they are ugly, and I like my solutions to be clean. On linux I use this: bool StdinOpen() { FILE* handle = popen("test -p /dev/stdin", "r"); return pclose(handle) == 0; } I know that I should add more error handling, but it's besides the point. On windows I use this: bool StdinOpen() { static HANDLE handle = GetStdHandle(STD_INPUT_HANDLE); DWORD bytes_left; PeekNamedPipe(handle, NULL, 0, NULL, &bytes_left, NULL); return bytes_left; } That is fine for linux, but I want to know what