eof

C Primer Plus 第8章 字符输入/输出和输入确认 8.3 终止键盘输入

帅比萌擦擦* 提交于 2019-12-05 20:56:08
8.3.1 文件、流和键盘输入 文件是一块存储信息的存储器区域。 通常,文件被保存在某种类别的永久存储器上,例如软盘、硬盘或磁盘。 具有强大、灵活等特点的C语言具有许多打开、读取、写入和关闭文件的库函数。在一个级别上,它可以使用宿主操作系统的基本文件工具来处理文件。这被称为低级I/O。由于计算机系统之间存在许多差异,所以不可能创建一个通用的低级I/O函数标准库。然而, C还以第二种级别处理文件,称为标准I/O包。这包括创建用于处理文件的I/O函数的标准模型和标准集。 使用标准I/O包时,就屏蔽掉了这些差异。因此,要检查一个换行符,您可以使用if(ch=='\n')。如果该系统实际上使用回车/换行字符的组合,则I/O函数自动在两种表示法之间来回转换。 从概念上讲,C程序处理一个流而不是直接处理文件。流(stream)是一个理想化的数据流,实际输入或输出映射到这个数据流。这意味着具有不同属性的多种类型的输入由流表示,会具有更多统一的属性。于是打开文件的过程就成为将流与文件相关联,并通过流进行读写的过程。 第13单详细的讨论了文件。对本章来说,仅需注意 C对待输入和输出设备与其对待存储设备上的普通文件相同 。特别的是, 键盘和显示设备作为每个C程序自动打开的文件来对待 。 键盘输入由一个被称为stdin的流表示,而到屏幕上的输出由一个被称为stdout的流表示。 getchar()

Visual Studio - blank line at the end of each new file

*爱你&永不变心* 提交于 2019-12-05 16:28:52
问题 Why does VS add a blank line at the end of each new file I create? I use VS to create .NET projects (not C++ or something). Is there any special reason? Historical compatibility with compilers and parsers? Can I disable this?? 回答1: It is recommended to have a blank line at the end of each file for navigability purposes. Think what happens if someone opens your code with vim for example and uses a keymap to jump from empty line to empty line. Or, if he decides to add another portion of code at

Behavior of using \\Z vs \\z as Scanner delimiter

我的未来我决定 提交于 2019-12-05 11:31:00
[Edit] I found the answer, but I can't answer the question due to restrictions on new users. Either way, this is a known bug in Java. http://bugs.java.com/bugdatabase/view_bug.do?bug_id=8028387 I'm trying to read a file into a string in Java 6 on 64 bit ubuntu. Java is giving me the very strange result that with "\\Z" it reads the entire file, but with "\\z" it reads the entire string up to 1024 characters. I've read the Java 6 API for all the classes and I am at a loss. Description of \Z and \z can be found at: http://docs.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html#lt What

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

早过忘川 提交于 2019-12-05 11:26:43
问题 This question already has answers here : Why do I have to press Ctrl+D twice to close stdin? (5 answers) Closed 5 years ago . 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

C# - Stream/FileStream EOF

那年仲夏 提交于 2019-12-05 07:09:23
Is anyone familiar with a way to find out you're at the end of the file? I'm using BinaryReader and tried PeekChar - but it throws an exception. Any other suggestions? Thanks. From a Stream , if you Read(buffer, offset, count) you'll get a non-positive result, and if you Peek() you'll get a negative result. With a BinaryReader , the documentation suggests that PeekChar() should return negative: Return Value Type: System.Int32 The next available character, or -1 if no more characters are available or the stream does not support seeking. are you sure this isn't a corrupt stream? i.e. the

How to detect EOF in awk?

孤人 提交于 2019-12-05 01:49:52
Is there a way to determine whether the current line is the last line of the input stream? You've got two options, both kind of messy. Store a copy of every current line in a temp variable, and then use the END block to process it. Use the system command to run "wc -l | getline" in the BEGIN block to get the number of lines in the file, and then count up the that value. You might have to play with #2 a little to get it to run, but it should work. Its been a while since I've done any awk. uriel The special END pattern will match only after the end of all input . Note that this pattern can't be

C Programming: EOF as a character

…衆ロ難τιáo~ 提交于 2019-12-05 01:06:45
问题 When programming C for the command console, what happens when you have a function that tries to use SCANF to ask user input for a CHAR variable, and the user types CTRL + Z (EOF) and hits enter? For example: char promptChar() { char c; printf("Enter a character: "); scanf("%c", &c); return c; } If the user types CTRL + Z and hits enter, what will promptChar() return? Because if I understand EOF, it's an int. 回答1: First things first: SCANF is not defined by the language. CHAR is not defined by

Fortran is reading beyond endfile record

元气小坏坏 提交于 2019-12-04 23:54:32
问题 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

How to check for EOF in Python?

只谈情不闲聊 提交于 2019-12-04 20:02:52
问题 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()

EOF, looping forever when the last line is read. c++

不问归期 提交于 2019-12-04 19:13:46
void ReadTheData(ifstream& in_stream, ofstream& out_stream) { using namespace std; int id; char name[100] = ""; double balance; in_stream >> id >> name >> balance; while(name[0] || ! in_stream.eof()) // Continue looping until eof or name is not present. { cout << id << '\t' << name << '\t' << balance << endl; in_stream >> id >> name >> balance; } } the Balance.txt has format style: 4248749 Kelly_E 460.02 8898693 Kelly_George_MD 1764.68 4597789 Lambert_Richard 1571.79 When I run the code, the last line in file Balance.txt goes into an infinite loop, how do I stop the infinite loop? Somehow this