eof

Why do I have to type ctrl-d twice? [duplicate]

时间秒杀一切 提交于 2019-12-03 23:19:24
This question already has answers here : Closed 5 years ago . Why do I have to press Ctrl+D twice to close stdin? (5 answers) For my own amusement, I've cooked up a python script that allows me to use python for bash one-liners; Supply a python generator expression; and the script iterates over it. Here's the script: DEFAULT_MODULES = ['os', 're', 'sys'] _g = {} for m in DEFAULT_MODULES: _g[m] = __import__(m) import sys sys.stdout.writelines(eval(sys.argv[1], _g)) And here's how you might use it. $ groups | python pype.py '(l.upper() for l in sys.stdin)' DBORNSIDE $ For the intended use, it

Common misconception : Files have an EOF char at their end

青春壹個敷衍的年華 提交于 2019-12-03 20:33:50
I was reading "PC assembly language by Carter" and I saw this phrase in footnote of page 32 which made me so confused ! If we assume that files may have not EOF at their end (as the book says) is a correct statement, then how can we figure out where is end of a file ? and it also arises another question : does fseek use EOF to go back and forth in file ? PC => ^Z : EOF in the olde PC-days the ctrl-Z was the signal in a file for EOF. under UNIX and other modern systems: after reading behind the stat.st_size, EOF is signalled sietschie From http://www.drpaulcarter.com/cs/common-c-errors.php :

Fortran is reading beyond endfile record

只谈情不闲聊 提交于 2019-12-03 15:54:19
I'm trying to read some data from a file, and the endfile record detection is important to stop reading. However, depending of the array dimensions of the array used to read data, I cannot detect properly the endfile record and my Fortran program stops. The program is below: !integer, dimension(3) :: x ! line 1.1 !integer, dimension(3,10) :: x ! line 1.2 integer, dimension(10,3) :: ! line 1.3 integer :: status,i=1 character(len=100) :: error open( 30, file='data.dat', status='old' ) do print *,i !read( 30, *, iostat=status, iomsg=error ) x ! line 2.1 !read( 30, *, iostat=status, iomsg=error )

How to check for EOF in Python?

人走茶凉 提交于 2019-12-03 12:52:39
How do I check for EOF in Python? I found a bug in my code where the last block of text after the separator isn't added to the return list. Or maybe there's a better way of expressing this function? Here's my code: def get_text_blocks(filename): text_blocks = [] text_block = StringIO.StringIO() with open(filename, 'r') as f: for line in f: text_block.write(line) print line if line.startswith('-- -'): text_blocks.append(text_block.getvalue()) text_block.close() text_block = StringIO.StringIO() return text_blocks You might find it easier to solve this using itertools.groupby . def get_text

In python, how to check the end of standard input streams (sys.stdin) and do something special on that

有些话、适合烂在心里 提交于 2019-12-03 11:22:32
I want to do something like: for line in sys.stdin: do_something() if is **END OF StdIn**: do_something_special() After a few tries, for now I am doing this: while True: try: line = sys.stdin.next() print line, except StopIteration: print 'EOF!' break Or with this: while True: line = sys.stdin.readline() if not line: print 'EOF!' break print line, I think both above ways are very similar. I want to know is there a more elegant (pythonic) way to do this? Early failed tries: I first tried to catch the StopIteration from inside or outside of a for loop, but I soon realize that since the

what does rdstate() return value means?

≡放荡痞女 提交于 2019-12-03 08:35:38
istream& Read(istream &is) { std::string buf; while (is >> buf) { cout << is.eofbit << " " << is.failbit << " " << is.badbit << endl; cout << is.rdstate() << endl; cout << buf << endl; } cout << is.eofbit << " " << is.failbit << " " << is.badbit << endl; cout << is.rdstate() << endl; is.clear(); cout << is.eofbit << " " << is.failbit << " " << is.badbit << endl; cout << is.rdstate() << endl; return is; } If I input normal characters like "test",the output is 1 2 4 0 . Then I type CTRL+Z (windows),the output is 1 2 4 3 1 2 4 0 . Question : 1. what does rdstate() return value means? (Why does it

sys.stdin does not close on ctrl-d

时间秒杀一切 提交于 2019-12-03 08:05:51
I have the following code in program.py: from sys import stdin for line in stdin: print line I run, enter lines, and then press Ctrl + D , but the program does not exit. This does work: $ printf "echo" | python program.py Why does the program not exit when I press Ctrl + d ? I am using the Fedora 18 terminal. Armin Rigo Ctrl + D has a strange effect. It doesn't close the input stream, but only causes a C-level fread() to return an empty result. For regular files such a result means that the file is now at its end, but it's acceptable to read more, e.g. to check if someone else wrote more data

EOF Error in Imaplib

你离开我真会死。 提交于 2019-12-03 08:02:44
I am programming a python applet that watches the unread count of the email boxes for my workplace, and ran into an EOF error when I try to use any imaplib methods after the applet sits idle for about 10 minutes. Everything works fine until the applet has been alive for more than 10 minutes. Here is the relevant code for the imaplib object. conn = imaplib.IMAP4_SSL("imap.gmail.com", 993) def loginIMAP (imapObj): # Login to Helpdesk Google Apps Email account using encryption imapObj.login(base64.b64decode("usrEncryption"), base64.b64decode("pwdEncrytion")) return(getUnread(imapObj)) def

Linux - check if there is an empty line at the end of a file [duplicate]

夙愿已清 提交于 2019-12-03 07:14:54
This question already has an answer here: How to detect file ends in newline? 6 answers NOTE: this question used to be worded differently, using “with/out newline” instead of “with/out empty line” I have two files, one with an empty line and one without: File: text_without_empty_line $root@kali:/home#cat text_without_empty_line This is a Testfile This file does not contain an empty line at the end $root@kali:/home# File: text_with_empty_line $root@kali:/home#cat text_with_empty_line This is a Testfile This file does contain an empty line at the end $root@kali:/home# Is there a command or

Why multiple EOF enters to end program?

て烟熏妆下的殇ゞ 提交于 2019-12-02 19:17:09
问题 Trying to understand the behavior of my code. I'm expecting Ctrl-D to lead to the program printing the array and exiting, however it takes 3 presses, and it enters the while loop after the second press. #include <stdio.h> #include <stdlib.h> void unyon(int p, int q); int connected(int p, int q); int main(int argc, char *argv[]) { int c, p, q, i, size, *ptr; scanf("%d", &size); ptr = malloc(size * sizeof(int)); while((c = getchar()) != EOF){ scanf("%d", &p); scanf("%d", &q); printf("p = %d, q