eof

C++ Reading file backwards from the end of the file

不羁的心 提交于 2019-12-01 18:50:22
I am trying to write a program with a menu that reads from a text file a few different ways. I'm just working on menu option #2 still (reading backwards from the end of the file), but I can't wrap my head around what I'm doing wrong. I've been at this for a few days now and just can't find any good resources to help on this. Any help would be appreciated. #include <iostream> #include <string> #include <iomanip> #include <istream> #include <math.h> #include <fstream> using namespace std; const int SIZE = 20; typedef char String[SIZE]; //prototypes void Menu(int &); void ReadFile(ifstream &);

How to see if a Reader is at EOF?

你说的曾经没有我的故事 提交于 2019-12-01 15:52:23
My code needs to read in all of a file. Currently I'm using the following code: BufferedReader r = new BufferedReader(new FileReader(myFile)); while (r.ready()) { String s = r.readLine(); // do something with s } r.close(); If the file is currently empty, though, then s is null, which is no good. Is there any Reader that has an atEOF() method or equivalent? A standard pattern for what you are trying to do is: BufferedReader r = new BufferedReader(new FileReader(myFile)); String s = r.readLine(); while (s != null) { // do something with s s = r.readLine(); } r.close(); The docs say: public int

How to see if a Reader is at EOF?

为君一笑 提交于 2019-12-01 14:59:11
问题 My code needs to read in all of a file. Currently I'm using the following code: BufferedReader r = new BufferedReader(new FileReader(myFile)); while (r.ready()) { String s = r.readLine(); // do something with s } r.close(); If the file is currently empty, though, then s is null, which is no good. Is there any Reader that has an atEOF() method or equivalent? 回答1: A standard pattern for what you are trying to do is: BufferedReader r = new BufferedReader(new FileReader(myFile)); String s = r

How to stimulate EOF without preceding newline in C

情到浓时终转凉″ 提交于 2019-12-01 12:14:42
Lets say I have the below C code: int getLine (char line[]) { int c, i=0; while( (c=getchar()) != EOF ) line[i++]=c; line[i++] = c; return i; } >> Enter: 007 >> ^Z >> Output: If we closely observe the way I give output above, I am pressing Enter before stimulating EOF. This means, the length of string is 4 not 3 (excluding EOF). When I am doing my exercises, I am really facing some trouble with that extra \n. How do I stimulate EOF without newline? Is it possible at all? >> Enter: 007^Z >> ^Z >> Output: length=6 Converting my comment into an answer: On which platform? On Unix and derivatives,

Java, need a while loop to reach eof. i.e.while !eof, keep parsing

最后都变了- 提交于 2019-12-01 11:32:32
I currently have a working parser. It parses a file once(not what I want it to do) and then outputs parsed data into a file. I need it to keep parsing and appending to the same output file until the end of the input file. Looks something like this. try { // my code parsing the data and appending to eof of output. (works) } catch (EOFException eof){ } Everything is done except the while loop. It only parses once when I need it to keep parsing. I'm looking for a while loop function to reach eof. I'm also using a DataInputStream. Is there some sort of DataInputStream.hasNext function?

Testing getchar() == EOF doesn't work as expected

不羁岁月 提交于 2019-12-01 11:22:05
I have an assignment to "Write a C program that allows a user to enter up to 20 integers (it will stop accepting numbers based on a sentinel value or based on the 20-integer limit being reached). The program should then display the numbers in the reverse order of entry." I decided to make my sentinel value "EOF" (or CTRL+D / CRTL+Z). My code has some really erratic behavior: You have to press the EOF keys twice (which also creates a blank entry that is counted int the array. The first digit from the first entry gets truncated. Everything else seems to work OK but, this is clearly not the

Why do I need to press CTRL+D twice to break out of `while ((c=getchar())!=EOF)` in Ubuntu 14.10?

不想你离开。 提交于 2019-12-01 11:17:29
I am new to C Programming and Ubuntu. I was reading the "The C Programming Language" by D.M Ritchie where I found the following code: #include <stdio.h> int main() { int c; int nc=0; while((c = getchar()) != EOF) { nc++; } printf("%d Characters \n",nc); return 0; } But while running the program I enter "Hello" ,then CTRL+D twice to get the actual number of characters which is 5. But when I enter "Hello" then CTRL+D once , nothing happens, the terminal still waits for input. Why? Spikatrix Quoting @Veritas's comment , On linux Ctrl-D only works when the buffer is already empty otherwise it just

Huffman encoding - header & EOF

偶尔善良 提交于 2019-12-01 10:59:51
I am currently working on implementing a program based on the huffman algorithm in Java, and I am at the stage where I need to output the encoded content to a file. I am a bit confused about how to implement the header and eof needed for decoding. For my header at the moment I have all the unique values that occur from the input file and their frequency, but on some articles I have seen people do it with 0 or 1 represents the nodes and then the frequency (which I am a bit puzzled by as it doesn't say what the symbol is). Also, for the EOF as I understand it I encode it like the symbols so it

Java, need a while loop to reach eof. i.e.while !eof, keep parsing

*爱你&永不变心* 提交于 2019-12-01 08:33:22
问题 I currently have a working parser. It parses a file once(not what I want it to do) and then outputs parsed data into a file. I need it to keep parsing and appending to the same output file until the end of the input file. Looks something like this. try { // my code parsing the data and appending to eof of output. (works) } catch (EOFException eof){ } Everything is done except the while loop. It only parses once when I need it to keep parsing. I'm looking for a while loop function to reach eof

Testing getchar() == EOF doesn't work as expected

佐手、 提交于 2019-12-01 08:01:59
问题 I have an assignment to "Write a C program that allows a user to enter up to 20 integers (it will stop accepting numbers based on a sentinel value or based on the 20-integer limit being reached). The program should then display the numbers in the reverse order of entry." I decided to make my sentinel value "EOF" (or CTRL+D / CRTL+Z). My code has some really erratic behavior: You have to press the EOF keys twice (which also creates a blank entry that is counted int the array. The first digit