eof

Actual implementation of EOF different from -1

只愿长相守 提交于 2019-12-01 02:04:29
问题 POSIX defines EOF as a macro expanding to a negative value: The header shall define the following macro which shall expand to an integer constant expression with type int and a negative value: EOF End-of-file return value. In every implementation I could find, EOF is always defined as -1 . Although the standard does allow for different values, I could not find any specific implementation where that happens, and I'd like to find one for testing purposes. 1 1 I could make my own implementation,

Why do i have to input EOF 3 times when using fgets?

大憨熊 提交于 2019-12-01 01:29:48
So basically I want to copy everything i write to stdin (including newline char) to string for hash purposes. I managed to accomplish that and made small code to represent my problem. #include <stdio.h> #include <string.h> #include <stdlib.h> #define BUFFERSIZE 10000 int main() { char *myStr = calloc(1,1); char buffer[BUFFERSIZE]; while( fgets(buffer, BUFFERSIZE , stdin) != NULL ){ myStr = realloc(myStr, strlen(myStr)+1+strlen(buffer) ); strcat( myStr, buffer ); } printf("\n%s\n",myStr); } everything works when I enter some text then press ENTER and after I call EOF. But when I start program

Use getline() without setting failbit

China☆狼群 提交于 2019-12-01 00:29:04
问题 Is it possible use getline() to read a valid file without setting failbit ? I would like to use failbit so that an exception is generated if the input file is not readable. The following code always outputs basic_ios::clear as the last line - even if a valid input is specified. test.cc: #include <iostream> #include <string> #include <fstream> using namespace std; int main(int argc, char* argv[]) { ifstream inf; string line; inf.exceptions(ifstream::failbit); try { inf.open(argv[1]); while

EOF exercise 1-6 K&R The C programming language

不羁岁月 提交于 2019-11-30 23:48:22
This is taken directly from the K&R book: The precedence of != is higher than that of = , which means that in the absence of parentheses the relational test != would be done before the assignment = . So the statement c = getchar() != EOF is equivalent to c = (getchar() != EOF) This has the undesired effect of setting c to 0 or 1, depending on whether or not the call of getchar returned end of file. (More on this in Chapter 2.) Exercise 1-6. Verify that the expression getchar() != EOF is 0 or 1. I am having trouble understanding how to do this exercise as well as understanding what is going on

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

心已入冬 提交于 2019-11-30 20:51:46
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 not gerenate an EOF. I tried to implement this myself, but did not find a convenient solution. The

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

左心房为你撑大大i 提交于 2019-11-30 20:33:09
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) 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 incidentally, 0-sized read is how the end of file condition on ordinary files is detected. That's why the first EOF in

std::getline throwing when it hits eof

会有一股神秘感。 提交于 2019-11-30 20:30:19
std::getline throws exception when it gets an eof . this is how I am doing. std::ifstream stream; stream.exceptions(std::ifstream::failbit|std::ifstream::badbit); try{ stream.open(_file.c_str(), std::ios_base::in); }catch(std::ifstream::failure e){ std::cout << "Failed to open file " << _file.c_str() << " for reading" << std::endl; } while(!stream.eof()){ std::string buffer = ""; std::getline(stream, buffer); //process buffer //I do also need to maintain state while parsing } In the above code getline is throwing exception as it gets eof How to handle this situation ? EDIT std::string buffer =

ProbIem with EOF in C

与世无争的帅哥 提交于 2019-11-30 10:14:42
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 seems to help. All cleaning attempts have resulted in an infinite loop, and without cleaning, adding the

How to read user input until EOF?

こ雲淡風輕ζ 提交于 2019-11-30 05:43:14
My current code reads user input until line-break. But I am trying to change that to a format, where the user can write input until strg+d to end his input. I currently do it like this: input = raw_input ("Input: ") But how can I change that to an EOF-Ready version? Use file.read : input_str = sys.stdin.read() According to the documentation: file.read([size]) Read at most size bytes from the file (less if the read hits EOF before obtaining size bytes). If the size argument is negative or omitted, read all data until EOF is reached. >>> import sys >>> isinstance(sys.stdin, file) True BTW, dont'