eof

Detecting EOF with fgets() where filesteam is stdin

China☆狼群 提交于 2019-12-07 17:23:07
问题 Bit of background, I'm writing a program that plays the game "boxes" it runs in linux command line and is written in C. There's a prompt that waits for user input and then is read with fgets() and interpretted etc. As part of the task specification I have to return a specific error if I reach "End of file while waiting for user input". I understand that fgets() returns null when it reaches EOF... but say I have fgets(input,max_buffer,stdin); in a prompt loop if the user exits prematurely say

How to detect EOF in awk?

本秂侑毒 提交于 2019-12-06 20:33:41
问题 Is there a way to determine whether the current line is the last line of the input stream? 回答1: 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.

confused by control flow execution in C++ Primer example

…衆ロ難τιáo~ 提交于 2019-12-06 12:18:09
I am going through C++ Primer (5th ed). In section 1.4.4, there is the following example: #include <iostream> int main() { // currVal is the number we're counting; we'll read new values into val int currVal = 0, val = 0; // read first number and ensure that we have data to process if (std::cin >> currVal) { int cnt = 1; // store the count for the current value we're processing while (std::cin >> val) { // read the remaining numbers if (val == currVal) // if the values are the same ++cnt; // add 1 to cnt else { // otherwise, print the count for the previous value std::cout << currVal << "

Why isEOF doesn't work?

时光毁灭记忆、已成空白 提交于 2019-12-06 11:19:00
Here is minimal complete example: import Control.Monad import System.IO loop :: IO () loop = do line <- getLine putStrLn line eof <- isEOF unless eof loop main = loop This program is supposed to read a line, print it out, stop if there is 'end of file' character in stdin . It doesn't leave the loop at all. If I put eof <- isEOF before putStrLn line the program behaves very strange (try it!). I cannot get it at all: how putStrLn can possibly affect input stream and why doesn't the program terminate when I put 'end of file' character into stream (with Ctrl+D )? Description of program's behavior

Erlang: How to pipe stdin input from a file to an erlang program and match eof?

社会主义新天地 提交于 2019-12-06 07:56:46
问题 How to pipe input from a file as stdin to an erlang program running in the shell as well as standalone? I have a file hr.erl and I compile it from the shell. There is a function in it which accepts input from stdin using io:fread() . I have written a case expression with guards where if it matches {ok, [0]} it should terminate. Instead of 0 , I actually need it to be eof . How to send eof when running in a shell? I have a file inp.txt with values 1 2 3 and 0 on each line. How can I pass it

istream::peek curious behavior wrt. EOF

試著忘記壹切 提交于 2019-12-06 06:18:27
I've just encountered a curious situation in C++. I was doing something like: istream in; // ... in.get(); // get a char (which turns out to be the last) // curiously ios::eof bit doesn't get set just yet c = in.peek(); // attempt to peek, return EOF and now set ios::eof bit if(c == EOF) { // realize shouldn't have gotten the last char for some reason in.unget(): // attempt to unget, *fails* (because ios:eof bit was set) } Now I'm curious why peek sets the eof bit; I find this highly unintuitive. It's supposed to just peek not actually consume anything and shouldn't change the stream state.

read strings from input till EOF

放肆的年华 提交于 2019-12-06 05:57:20
I have gone through many posts on SO , but still i am not able to resolve the issue . I have to read : text pattern1 pattern2 from standard input , there are many text and patterns . Code : string t,p1,p2; while(getline(cin, t)) { cin>>p1; cin>>p2; cout<<"text is = "<<t<<"\np1 is = "<<p1<<"\np2 is = "<<p2<<endl; } Input file : hammer ham mer gogoa go z gogoa g o Output : text is = hammer p1 is = ham p2 is = mer text is = p1 is = gogoa p2 is = go text is = p1 is = z p2 is = gogoa text is = p1 is = g p2 is = o Marco A. If you're using getline after cin >> something , you need to flush the

Send command and exit using python pty pseudo terminal process

一曲冷凌霜 提交于 2019-12-06 03:37:05
Using python pty module, i want to send some commands to the terminal emulator, using a function as stdin (as pty module wants), and then force quitting. I thought about something like import pty cmnds = ['exit\n', 'ls -al\n'] # Command to send. I try exiting as last command, but it doesn't works. def r(fd): if cmnds: cmnds.pop() # It seems is not executing sent commands ('ls -al\n') else: # Can i quit here? Can i return EOF? pass pty.spawn('/bin/sh', r) Thank you Firstly, the pty module does not allow you to communicate with the terminal emulator Python is running in. Instead, it allows

Detecting EOF with fgets() where filesteam is stdin

不想你离开。 提交于 2019-12-06 01:34:08
Bit of background, I'm writing a program that plays the game "boxes" it runs in linux command line and is written in C. There's a prompt that waits for user input and then is read with fgets() and interpretted etc. As part of the task specification I have to return a specific error if I reach "End of file while waiting for user input". I understand that fgets() returns null when it reaches EOF... but say I have fgets(input,max_buffer,stdin); in a prompt loop if the user exits prematurely say with CTRL+C or CTRL+D does this mean that input == NULL? Can I even detect when a user does this with

read data from file till end of line in C/C++

久未见 提交于 2019-12-06 00:53:03
问题 It is common to read until end of file, but I am interested in how could I read data (a series of numbers) from a text file until the end of a line ? I got the task to read several series of numbers from a file, which are positioned in new lines. Here is an example of input: 1 2 53 7 27 8 67 5 2 1 56 9 100 2 3 13 101 78 First series: 1 2 53 7 27 8 Second one: 67 5 2 Third one: 1 56 9 100 2 3 13 101 78 I have to read them separately from file, but each one till the end of line. I have this