eof

Can we write an EOF character ourselves?

巧了我就是萌 提交于 2019-11-26 16:32:19
Most of the languages like C++ when writing into a file, put an EOF character even if we miss to write statements like : filestream.close However is there any way, we can put the EOF character according to our requirement, in C++, for an instance. Or any other method we may use apart from using the functions provided in C++. If you need to ask more of information then kindly do give a comment. Thanks in advance. EDIT: Thanks for your support but here's an addition to this question: What if, we want to trick the OS and place an EOF character in a file and write some data after the EOF so that

How to use EOF to run through a text file in C?

泪湿孤枕 提交于 2019-11-26 15:00:53
I have a text file that has strings on each line. I want to increment a number for each line in the text file, but when it reaches the end of the file it obviously needs to stop. I've tried doing some research on EOF, but couldn't really understand how to use it properly. I'm assuming I need a while loop, but I'm not sure how to do it. How you detect EOF depends on what you're using to read the stream: function result on EOF or error -------- ---------------------- fgets() NULL fscanf() number of succesful conversions less than expected fgetc() EOF fread() number of elements read less than

while ((c = getchar()) != EOF) Not terminating

有些话、适合烂在心里 提交于 2019-11-26 14:49:55
问题 I've been reading "The C Programming Language" and I got to this part of inputs and outputs. I've read other threads saying that the console doesn't recognize enter as EOF . So that I should use CTRL + Z in Windows or CTRL + D in Unix (neither of those is working for me). I also read other people asking the same saying they could make it work, the problem in their codes was syntax not the program not terminating. Is there another solution? This is the code: #include <stdio.h> main() { int nb,

ifstream not reading EOF character

匆匆过客 提交于 2019-11-26 14:33:44
问题 I am creating a program (In C++) that takes an ASCII file and reads a few values from each line until it reaches the end of the file. I am using ifstream to read the file, and I have never had problems with it stopping when I use the ifstream.eof() method. This time, however, even though it found the eof character in my test case, when I analyzed my other files, it is infinite looping because it never finds the eof character. Is this a coding issue, or an issue with my files? string line = ""

What&#39;s the real reason to not use the EOF bit as our stream extraction condition?

我怕爱的太早我们不能终老 提交于 2019-11-26 14:23:55
问题 Inspired by my previous question A common mistake for new C++ programmers is to read from a file with something along the lines of: std::ifstream file("foo.txt"); std::string line; while (!file.eof()) { file >> line; // Do something with line } They will often report that the last line of the file was read twice. The common explanation for this problem (one that I have given before) goes something like: The extraction will only set the EOF bit on the stream if you attempt to extract the end

Why do I need to type Ctrl-D twice to mark end-of-file?

假如想象 提交于 2019-11-26 13:59:28
问题 char **query; query = (char**) malloc ( sizeof(char*) ); int f=0; int i=0,j=0,c; while((c=getchar())!=EOF) { if(!isalpha(c)) continue; if(f==1) query=(char**) realloc(query,(i+1)*sizeof(char*)); query[i]=(char*) malloc(sizeof(char)); query[i][j]=c; j++; while( (c=getchar())!=EOF&&c!=' '&&c!='\t' ) { query[i]=(char*) realloc(query[i],(j+1)*sizeof(char)); query[i][j]=c; ++j; } query[i][j]='\0'; printf("%s\n",query[i]); if(c==EOF){ break; } ++i; f=1; j=0; } I want the above code snippet to read

I&#39;m trying to understand getchar() != EOF

怎甘沉沦 提交于 2019-11-26 12:11:27
问题 I\'m reading The C Programming Language and have understood everything so far. However when I came across the getchar() and putchar() , I failed to understand what is their use, and more specifically, what the following code does. main() { int c; while ((c = getchar()) != EOF) putchar(c); } I understand the main() function, the declaration of the integer c and the while loop. Yet I\'m confused about the condition inside of the while loop. What is the input in this C code, and what is the

How does ifstream&#39;s eof() work?

让人想犯罪 __ 提交于 2019-11-26 11:20:33
#include <iostream> #include <fstream> int main() { std::fstream inf( "ex.txt", std::ios::in ); while( !inf.eof() ) { std::cout << inf.get() << "\n"; } inf.close(); inf.clear(); inf.open( "ex.txt", std::ios::in ); char c; while( inf >> c ) { std::cout << c << "\n"; } return 0; } I'm really confused about eof() function. Suppose that my ex.txt's content was: abc It always reads an extra character and shows -1 when reading using eof() . But the inf >> c gave the correct output which was 'abc'? Can anyone help me explain this? -1 is get 's way of saying you've reached the end of file. Compare it

What is the perfect counterpart in Python for “while not EOF”

白昼怎懂夜的黑 提交于 2019-11-26 11:15:07
To read some text file, in C or Pascal, I always use the following snippets to read the data until EOF: while not eof do begin readline(a); do_something; end; Thus, I wonder how can I do this simple and fast in Python? Loop over the file to read lines: with open('somefile') as openfileobject: for line in openfileobject: do_something() File objects are iterable and yield lines until EOF. Using the file object as an iterable uses a buffer to ensure performant reads. You can do the same with the stdin (no need to use raw_input() : import sys for line in sys.stdin: do_something() To complete the

C : How to simulate an EOF?

我们两清 提交于 2019-11-26 10:29:44
I am currently reading K&R's book and typing in the examples from the first section, and there are a couple of examples such as this: while((c = getchar()) != EOF) { //do something } I am testing these examples on a Windows box and thus running the compiled exe files from the cmd prompt. To test the example above, how do I simulate an EOF ? That is, basically how can I make the loop stop when testing the example from the command prompt? Greg Hewgill To enter an EOF, use: ^Z ( Ctrl Z ) in Windows ^D on Unix-like systems Refer EOF Windows: Ctrl+Z Unix :Ctrl+D First, press: Ctrl^X, next: Ctrl^D