eof

What is value of EOF and '\\0' in C

随声附和 提交于 2019-11-26 21:56:03
I know that EOF and '\0' are of type integers, but if so shouldn't they have a fixed value? I printed both and got -1 for EOF and 0 for '\0' . But are these values fixed? I also had this int a=-1; printf("%d",a==EOF); //printed 1 Are the value for EOF and '\0' fixed integers? CB Bailey EOF is a macro which expands to an integer constant expression with type int and an implementation dependent negative value but is very commonly -1. '\0' is a char with value 0 in C++ and an int with the value 0 in C. The reason why printf("%d",a==EOF); resulted in 1 was because you didn't assign the value EOF

How to find out whether a file is at its `eof`?

霸气de小男生 提交于 2019-11-26 20:22:38
fp = open("a.txt") #do many things with fp c = fp.read() if c is None: print 'fp is at the eof' Besides the above method, any other way to find out whether is fp is already at the eof? fp.read() reads up to the end of the file, so after it's successfully finished you know the file is at EOF; there's no need to check. If it cannot reach EOF it will raise an exception. When reading a file in chunks rather than with read() , you know you've hit EOF when read returns less than the number of bytes you requested. In that case, the following read call will return the empty string (not None ). The

Checking for an empty file in C++ [duplicate]

非 Y 不嫁゛ 提交于 2019-11-26 20:13:47
This question already has an answer here: How do I check to see if a text file is empty or does not exist in c++? 2 answers Is there an easy way to check if a file is empty. Like if you are passing a file to a function and you realize it's empty, then you close it right away? Thanks. Edit, I tried using the fseek method, but I get an error saying 'cannot convert ifstream to FILE *'. My function's parameter is myFunction(ifstream &inFile) Perhaps something akin to: bool is_empty(std::ifstream& pFile) { return pFile.peek() == std::ifstream::traits_type::eof(); } Short and sweet. With concerns to

How do you read scanf until EOF in C?

烂漫一生 提交于 2019-11-26 20:08:57
问题 I have this but once it reaches the supposed EOF it just repeats the loop and scanf again. int main(void) { char words[16]; while(scanf("%15s", words) == 1) printf("%s\n", words); return 0; } 回答1: Try: while(scanf("%15s", words) != EOF) You need to compare scanf output with EOF Since you are specifying a width of 15 in the format string, you'll read at most 15 char. So the words char array should be of size 16 ( 15 +1 for null char). So declare it as: char words[16]; 回答2: Scanf is pretty much

End of File in C++

佐手、 提交于 2019-11-26 19:09:38
I have a n X 2 matrix stored in a text file as it is. I try to read it in C++ nb_try=0; fin>>c_tmp>>gamma_tmp; while (!fin.eof( )) //if not at end of file, continue reading numbers { // store cs_bit.push_back(c_tmp); gammas_bit.push_back(gamma_tmp); nb_try++; // read fin>>c_tmp; assert(!fin.fail( )); // fail at the nb_try=n if(fin.eof( ))break; fin>>gamma_tmp; // get first number from the file (priming the input statement) assert(!fin.fail( )); } The first assert failes, i.e. fin.fail( ) is true, when nb_try==n, which happens when it tries to read the first number which does not exist. But how

Why is it recommended to have empty line in the end of a source file?

喜欢而已 提交于 2019-11-26 18:49:54
问题 Some code style tools recommend this and I remember seeing some unix command line tools warning about missing empty line. What is the reasoning for having an extra empty line? 回答1: Many older tools misbehave if the last line of data in a text file is not terminated with a newline or carriage return / new line combination. They ignore that line as it is terminated with ^Z (eof) instead. 回答2: If you try to concatenate two text files together, you will be much happier if the first one ends with

C++ Issue with cin and CTRL + Z

我们两清 提交于 2019-11-26 18:39:02
问题 I'm reading c++ primer 5th and I have a little problem with an exercise: Read a sequence of words from cin and store the values a vector. After you’ve read all the words, process the vector and change each word to uppercase. Print the transformed elements, eight words to a line. My code is this: #include <iostream> #include <vector> #include <string> #include <cctype> using std::vector; using std::string; using std::cin; using std::cout; using std::endl; int main(){ vector<string> words;

How to use `while read` (Bash) to read the last line in a file if there’s no newline at the end of the file?

本小妞迷上赌 提交于 2019-11-26 18:01:58
问题 Let’s say I have the following Bash script: while read SCRIPT_SOURCE_LINE; do echo "$SCRIPT_SOURCE_LINE" done I noticed that for files without a newline at the end, this will effectively skip the last line. I’ve searched around for a solution and found this: When read reaches end-of-file instead of end-of-line, it does read in the data and assign it to the variables, but it exits with a non-zero status. If your loop is constructed "while read ;do stuff ;done So instead of testing the read

fgetc does not identify EOF [duplicate]

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-26 17:18:40
问题 This question already has answers here : Difference between int and char in getchar/fgetc and putchar/fputc? (2 answers) Closed last year . The program below runs fine on various Solaris/Linux flavours, but not on AIX. However, if I replace while(c!=EOF) with while(c!=0xff) on AIX it runs completely fine. Any thoughts? I checked the fgetc man page on AIX, and it should return the EOF constant! #include <stdio.h> #include<unistd.h> #include <string.h> int main() { char c; FILE *fp; fp = fopen(

Why is failbit set when eof is found on read?

半腔热情 提交于 2019-11-26 17:13:39
问题 I've read that <fstream> predates <exception> . Ignoring the fact that exceptions on fstream aren't very informative, I have the following question: It's possible to enable exceptions on file streams using the exceptions() method. ifstream stream; stream.exceptions(ifstream::failbit | ifstream::badbit); stream.open(filename.c_str(), ios::binary); Any attempt to open a nonexistent file, a file without the correct permissions, or any other I/O problem will results in exception. This is very