fflush

In bash, How can I force a flush of an incomplete line printed to the terminal

夙愿已清 提交于 2019-12-09 19:29:55
问题 I'm writing a script which does something like the following: echo -n "Doing stuff, wait for it... " do_stuff (($?==0)) && echo SUCCESS || echo FAILURE Excuse the poor bash skills. Anyway, the problem is that the first part of the line doesn't get printed until do_stuff is done - while it is important to me the user know what I'm running next. It is also important to me, since I'm pedantic, not to print a newline. So, the text is in the buffer and doesn't get flushed. This question is very

fflush(stdin) does not work compiled with gcc in cygwin but does compiled with visual studio 2010

柔情痞子 提交于 2019-12-08 18:56:28
I am (re-)learning programming and I started with C. My IDE (if I may say so) is cygwin (32Bit) and Visual-Studio 2010 both on Windows7. I am always compiling the code I write with gcc (cygwin) as well as with the VS2010 compiler. I do so, I guess, because I think it is a good way of learning. Anyway I just learned about fflush(stdin), i.e. flushing the stdin buffer. Seems a good functionality because else, using scanf appears to be a pain. So I wrote the code below based on an example from my text book. It compiles both with gcc in cygwin as well as with VS2010. When I run the VS compiled

Extract unique IPs from live tcpdump capture

大城市里の小女人 提交于 2019-12-06 14:32:29
问题 I am using the following command to output IPs from live tcpdump capture sudo tcpdump -nn -q ip -l | awk '{print $3; fflush(stdout)}' >> ips.txt I get the following output 192.168.0.100.50771 192.168.0.100.50770 192.168.0.100.50759 Need 2 things: Extract only the IPs, not the ports. Generate a file with unique IPs, no duplicated, and sorted if posible. Thank you in advance 回答1: To extract unique IPs from tcpdump you can use: awk '{ ip = gensub(/([0-9]+.[0-9]+.[0-9]+.[0-9]+).*/,"\\1","g",$3);

Clear input buffer after fgets() in C

*爱你&永不变心* 提交于 2019-12-06 02:09:34
问题 #include <stdio.h> int main() { char name[10]; for(int i=0;i<=10;i++) { printf("Who are you? "); if(fgets(name,10,stdin)!=NULL) printf("Glad to meet you, %s.\n",name); } return(0); } When I input something greater than 10 characters the loop skips. Who are you? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa Glad to meet you, aaaaaaaaa. Who are you? Glad to meet you, aaaaaaaaa. Who are you? Glad to meet you, aaaaaaaaa. Who are you? Glad to meet you, aaaaaaaaa. Who are you? Glad

Output not printing without fflush(stdout)

*爱你&永不变心* 提交于 2019-12-05 10:12:31
I don't understand why sometimes I need to use fflush() and sometimes not. My program is segfaulting at the moment and I am debugging it with print statements. When a program segfaults, does stdout not flush its buffer automatically? I don't understand why sometimes I need to use fflush() and sometimes not. Sometimes the stdio buffers are flushed sometimes they aren't. For example simply including a "\n" in the printed stuff will typically flush it (because stdout is by default line-buffered when attached to a terminal). When a program segfaults, does stdout not flush its buffer automatically

windows console program stdout is buffered when using pipe redirection

陌路散爱 提交于 2019-12-05 07:27:26
i have a long run server program(say, program A) which is written in QT/c++. the program is not so stable so i decide to write a python script to restart it if it crashes. the problem is that the program may started fail(if i gave it an in-use port), print the error and then just hang there without quitting, so i must monitor the stdout of the program and kill it on failed startup. this is a piece of my final code (well, in fact this is ok, you can just ignore it): self.subp = subprocess.Popen( r'.\A.exe -server %d' % portnum, stdout=subprocess.PIPE, bufsize=1) for line in iter(self.subp

How fo force subprocess to refresh stdout buffer?

╄→尐↘猪︶ㄣ 提交于 2019-12-05 06:01:09
Platform: windows 8.1 IDE: vs2013 use c/c++ Process A read stdout of subprocess using pipe redirect. but subprocess dont invoke fflush after printf, so processs A cant read anything from pipe before subprocess run to end. ps: I have souce code of subprecess, but its very hard to modify them. So whether Process A can force subprocess refresh stdout buffer to read something before subprocess run to end? (the same effective as fflush) 来源: https://stackoverflow.com/questions/22527010/how-fo-force-subprocess-to-refresh-stdout-buffer

Extract unique IPs from live tcpdump capture

蹲街弑〆低调 提交于 2019-12-04 20:52:26
I am using the following command to output IPs from live tcpdump capture sudo tcpdump -nn -q ip -l | awk '{print $3; fflush(stdout)}' >> ips.txt I get the following output 192.168.0.100.50771 192.168.0.100.50770 192.168.0.100.50759 Need 2 things: Extract only the IPs, not the ports. Generate a file with unique IPs, no duplicated, and sorted if posible. Thank you in advance To extract unique IPs from tcpdump you can use: awk '{ ip = gensub(/([0-9]+.[0-9]+.[0-9]+.[0-9]+).*/,"\\1","g",$3); if(!d[ip]) { print ip; d[ip]=1; fflush(stdout) } }' YOURFILE So your command to see unique IPs live would be

Can fseek(stdin,1,SEEK_SET) or rewind(stdin) be used to flush the input buffer instead of non-portable fflush(stdin)?

最后都变了- 提交于 2019-12-04 03:17:48
问题 Since I discovered fflush(stdin) is not a portable way to deal with the familiar problem of "newline lurking in the input buffer" ,I have been using the following when I have to use scanf : while((c = getchar()) != '\n' && c != EOF); But today I stumbled across this line which I had noted from cplusplus.com on fflush: fflush()...in files open for update (i.e., open for both reading and writting), the stream shall be flushed after an output operation before performing an input operation. This

Clearing the buffer when using Getchar (there must be a better way!)

梦想的初衷 提交于 2019-12-04 02:18:07
问题 I am writing a function where I only need to obtain a single digit, I decided to use getchar() instead of scanf(). However since I only need one character I did not use an array to obtain it. This causes a problem because I dont have a loop to find the '/n' character. To make sure my buffer is cleared before using getchar() again, I wrote this: while (getchar() != '\n'){} It works as I need it to, but is this the "correct" way to go about this problem? I tried using fflush(stdin), but it