eof

Why does getchar() recognize EOF only in the beginning of a line?

烈酒焚心 提交于 2019-11-30 05:07:25
问题 This example is from the K&R book #include<stdio.h> main() { long nc; nc = 0; while(getchar() != EOF) ++nc; printf("%ld\n", nc); } Could you explain me why it works that way. Thanks. ^Z^Z doesn't work either (unless it's in the beginning of a line) 回答1: Traditional UNIX interpretation of tty EOF character is to make blocking read return after reading whatever is buffered inside a cooked tty line buffer. In the start of a new line, it means read returning 0 (reading zero bytes), and

Bash script: save stream from Serial Port (/dev/ttyUSB0) to file until a specific input (e.g. eof) appears

主宰稳场 提交于 2019-11-30 05:03:58
问题 I need a bash script to read the data stream from a Serial Port (RS232 to USB adapter - Port: /dev/ttyUSB0). The data should be stored line by line in a file until a specific input (for example "eof") appears. I can give any external input to the Serial Port. Till now I use cat to read the data, which works fine. cat /dev/ttyUSB0 -> file.txt The problem is, that I need to finish the command myself by entering cntr+C, but I don't know exactly when the data stream ends and the ttyUSB0 file does

How to read from std::cin until the end of the stream?

亡梦爱人 提交于 2019-11-29 15:47:48
My problem is, that I want to read the input from std::cin but don't know how long the input is. Also I have to char and can't use std::string . There are two ways I have to handle: a) The user inputs text and when he hits [ENTER] the program stops reading. b) The user redirects std::cin to a file (like .\a.oput < file ) which can hold multiple lines. Edit: Just noticed that std::cin.eof() is always false also in the case of reading form a file. For a) I could read until \n occures. For b) Edit: No (I could read until std::cin.eof() occures.) But when I don't know whether I'm getting an a) or

ProbIem with EOF in C

给你一囗甜甜゛ 提交于 2019-11-29 15:15:22
问题 I'm writing a program which is supposed to read two strings that can contain line breaks and various other characters. Therefore, I'm using EOF (Ctrl-Z or Ctrl-D) to end the string. This works fine with the first variable, but with the second variable, however, this seems to be problematic as apparently something is stuck in the input buffer and the user doesn't get to type in anything. I tried to clean the buffer with while (getchar() != '\n'); and several similar variations but nothing

Java heap dump error with jmap command : Premature EOF

試著忘記壹切 提交于 2019-11-29 13:17:40
I have encountered below exception during execution of below command jmap -dump:format=b,file=heap_dump.bin <process_id> output: Dumping heap to <file_name> Exception in thread "main" java.io.IOException: Premature EOF at sun.tools.attach.HotSpotVirtualMachine.readInt(HotSpotVirtualMachine.java:248) at sun.tools.attach.LinuxVirtualMachine.execute(LinuxVirtualMachine.java:199) at sun.tools.attach.HotSpotVirtualMachine.executeCommand(HotSpotVirtualMachine.java:217) at sun.tools.attach.HotSpotVirtualMachine.dumpHeap(HotSpotVirtualMachine.java:180) at sun.tools.jmap.JMap.dump(JMap.java:242) at sun

Why two EOF needed as input? [duplicate]

蹲街弑〆低调 提交于 2019-11-29 12:22:25
This question already has an answer here: Canonical vs. non-canonical terminal input 1 answer When I run the code below, I use three inputs (in Ubuntu terminal): abc(Ctrl+D)(Ctrl+D) abc(Ctrl+D)(Enter)(Ctrl+D) abc(Enter)(Ctrl+D) The code reacts well in all cases. My question is: why in 1) and 2) I need two EOF? #include <iostream> int main() { int character; while((character=std::cin.get())!=EOF){} std::cout << std::endl << character << std::endl; } That's how the "EOF" character works (in "canonical" mode input, which is the default). It's actually never sent to the application, so it would be

How to process huge text files that contain EOF / Ctrl-Z characters using Python on Windows?

冷暖自知 提交于 2019-11-29 10:23:10
I have a number of large comma-delimited text files (the biggest is about 15GB) that I need to process using a Python script. The problem is that the files sporadically contain DOS EOF (Ctrl-Z) characters in the middle of them. (Don't ask me why, I didn't generate them.) The other problem is that the files are on a Windows machine. On Windows, when my script encounters one of these characters, it assumes it is at the end of the file and stops processing. For various reasons, I am not allowed to copy the files to any other machine. But I still need to process them. Here are my ideas so far:

Detecting EOF in C++ from a file redirected to STDIN

╄→尐↘猪︶ㄣ 提交于 2019-11-29 08:39:29
Executing the command: ./program < input.txt with the following code checking: string input; while(cin) { getline(cin, input); } The above code seems to generate an extra getline() call where input is empty. This happens regardless of whether or not there's a \n on the last line of input.txt. @Jacob had the correct solution but deleted his answer for some reason. Here's what's going on in your loop: cin is checked for any of the failure bits (BADBIT, FAILBIT) cin reports no problem because nothing has yet been read from the file. getline is called which detects end of file, setting the EOF bit

How to read user input until EOF in python?

佐手、 提交于 2019-11-29 08:04:41
I came across this problem in UVa OJ. 272-Text Quotes Well, the problem is quite trivial. But the thing is I am not able to read the input. The input is provided in the form of text lines and end of input is indicated by EOF. In C/C++ this can be done by running a while loop: while( scanf("%s",&s)!=EOF ) { //do something } How can this be done in python .? I have searched the web but I did not find any satisfactory answer. Note that the input must be read from the console and not from a file. You can use sys module: import sys complete_inout = sys.stdin.read() sys.stdin is a file like object