stdin

Effective stdin reading c programming

会有一股神秘感。 提交于 2020-01-11 07:13:22
问题 can anyone help me optimalize code for reading standard input. Here it is what I have now: unsigned char *msg; size_t msgBytes = 0; size_t inputMsgBuffLen = 1024; if ( (msg = (unsigned char *) malloc(sizeof(unsigned char) * inputMsgBuffLen) ) == NULL ) { quitErr("Couldn't allocate memmory!", EXIT_FAILURE); } for (int c; (c = getchar()) != EOF; msgBytes++) { if (msgBytes >= (inputMsgBuffLen)) { inputMsgBuffLen <<= 1; if ( ( msg = (unsigned char *)realloc(msg, sizeof(unsigned char) *

Golang reading from stdin, how to detect special keys (enter, backspace… etc)

帅比萌擦擦* 提交于 2020-01-10 20:07:10
问题 I have the following program that reads user input from stdin: var input string = "" exec.Command("stty", "-F", "/dev/tty", "cbreak", "min", "1").Run() exec.Command("stty", "-F", "/dev/tty", "-echo").Run() var b []byte = make([]byte, 1) for { input += string(b) } I want to place some kind of condition inside the for loop so that i can "break" when the user presses "enter" (for example) or remove one char from a string when the user presses (backspace). However, i can't figure out what the

How to inherit stdin and stdout in python by using os.execv()

前提是你 提交于 2020-01-09 11:45:35
问题 First, I wrote a c++ code as follows: #include <cstdio> int main() { int a,b; while(scanf("%d %d",&a,&b) == 2) printf("%d\n",a+b); return 0; } I use g++ -o a a.cpp to complie it. Afterwards, I wrote python code as follows: import os,sys sys.stdin = open("./data.in","r") sys.stdout = open("./data.out","w") pid = os.fork() if pid == 0: cmd = ["./a","./a"] os.execv(cmd[0],cmd) However, the data.out file contains nothing. That is to say, the child process did not inherit stdin and stdout from his

How can I reinitialize Perl's STDIN/STDOUT/STDERR?

╄→尐↘猪︶ㄣ 提交于 2020-01-09 09:37:32
问题 I have a Perl script which forks and daemonizes itself. It's run by cron, so in order to not leave a zombie around, I shut down STDIN,STDOUT, and STDERR: open STDIN, '/dev/null' or die "Can't read /dev/null: $!"; open STDOUT, '>>/dev/null' or die "Can't write to /dev/null: $!"; open STDERR, '>>/dev/null' or die "Can't write to /dev/null: $!"; if (!fork()) { do_some_fork_stuff(); } The question I have is: I'd like to restore at least STDOUT after this point (it would be nice to restore the

C read stdin buffer before it is submit

那年仲夏 提交于 2020-01-06 14:11:58
问题 It is likely that this type of scenario is not possible as I have not found this behavior documented anywhere but was curious if anyone had any tricks to accomplish something like this. Is it possible to determine the contents of the stdin buffer for a program before the user actually hits enter to submit the data? I am attempting to do this as I have built a simple terminal chat program which sends messages from one terminal to another for easy communication. The issue we have run into is

Unable to write to stdin in subprocess

空扰寡人 提交于 2020-01-06 08:43:16
问题 i am unable to pass in commands to stdin in python 3.2.5. I have tried with the following 2 approaches Also: This question is a continuation of a previous question. from subprocess import Popen, PIPE, STDOUT import time p = Popen([r'fileLoc/uploader.exe'],shell = True, stdout=PIPE, stdin=PIPE, stderr=STDOUT) p.stdin.write('uploader -i file.txt -d outputFolder\n') print (p.communicate()[0]) p.stdin.close() i also get numbers such as 96, 0, 85 returned to me when i try the code in the IDLE

bcrypt generates incorrect hashes - is my user-input processing correct?

心不动则不痛 提交于 2020-01-06 06:09:51
问题 I've written a short program in Go to generate a bcrypt password hash from a password provided via stdin. Minimal example below: package main import ( "bufio" "fmt" "golang.org/x/crypto/bcrypt" ) func main() { fmt.Println("Enter password:") reader := bufio.NewReader(os.Stdin) inputPassword, _ := reader.ReadString('\n') inputPasswordBytes := []byte(inputPassword) hashBytes, _ := bcrypt.GenerateFromPassword(inputPasswordBytes, bcrypt.DefaultCost) hashStr := string(hashBytes) fmt.Println(hashStr

How to pass a binary file as stdin to a Docker containerized Python script using argparse?

家住魔仙堡 提交于 2020-01-06 05:38:09
问题 Update based on Anthony Sottile's Answer I re-implemented his solution to simplify the problem. Lets take Docker and Django out of the equation. The goal is to use Pandas to read excel by both of the following methods: python example.py - < /path/to/file.xlsx cat /path/to/file.xlsx | python example.py - where example.py is reproduced below: import argparse import contextlib from typing import IO import sys import pandas as pd @contextlib.contextmanager def file_ctx(filename: str) -> IO[bytes]

How can I do stdin.close() with the new Streams API in Dart?

 ̄綄美尐妖づ 提交于 2020-01-05 07:41:30
问题 I ask user for input in my command line (dart:io) app. After I get my answer from the user, I want to unsubscribe from the Stream . Then, later, I may want to listen to it again (but with different listener, so pause() and resume() don't help me). On startup, I have this: cmdLine = stdin .transform(new StringDecoder()); Later, when I want to gather input: cmdLineSubscription = cmdLine.listen((String line) { try { int optionNumber = int.parse(line); if (optionNumber >= 1 && optionNumber <=

C - Using poll to multiplex between socket(s) and stdin - Server

做~自己de王妃 提交于 2020-01-05 07:30:24
问题 I'm writing a client server application and I'm using poll to multiplex between several client sockets and stdin, where I can insert commands (example: stop the server). I believe the structure (the "logic") of my code is correct, however it's not behaving the way I expect it to: struct pollfd pfd[NSERVER]; //defined as 10 pfd[0].fd = fileno(stdin); pfd[0].events = POLLIN; pfd[1].fd = socktfd; //server bind, listen socket pfd[1].events = POLLIN; struct sockaddr_storage remoteaddr; // client