eof

Python detecting EOF

允我心安 提交于 2020-01-30 08:16:46
问题 I'm trying to write an if statement that goes as such, while reading a csv file: if row = [] or EOF: do stuff I've searched online and couldn't find any way of doing this. Help? 回答1: with open(fname, 'rb') as f: for line in f: # line = line.strip(' \r\n') # to remove spaces and new line chars if needed if not line: do stuff do stuff The above is sufficient. To check if you are in the end of file you can also do: import os with open(fname, 'rb') as f: is_end = f.tell() == os.fstat(f.fileno())

Negated scanset in fscanf and EOF

谁都会走 提交于 2020-01-24 17:54:49
问题 I've got a comma-separated list of strings in my file: Name 1, Name 2, Name 3, I want to read those names skipping all commas. I've written the following loop: while(true) { if(fscanf(file, "%[^,],", my_string) != 1) { break; } //... } However, it is always executing one more time than it supposed to. Given 3 names in the file, the loop will execute its statements 4 times. Why is this happening? Does EOF indicator rank to my negated scanset [^,] ? If so, then how can I solve this issue? 回答1:

Negated scanset in fscanf and EOF

北城余情 提交于 2020-01-24 17:54:26
问题 I've got a comma-separated list of strings in my file: Name 1, Name 2, Name 3, I want to read those names skipping all commas. I've written the following loop: while(true) { if(fscanf(file, "%[^,],", my_string) != 1) { break; } //... } However, it is always executing one more time than it supposed to. Given 3 names in the file, the loop will execute its statements 4 times. Why is this happening? Does EOF indicator rank to my negated scanset [^,] ? If so, then how can I solve this issue? 回答1:

std::getline throwing when it hits eof

a 夏天 提交于 2020-01-21 04:48:47
问题 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

std::getline throwing when it hits eof

雨燕双飞 提交于 2020-01-21 04:48:45
问题 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

reading input till EOF in java

女生的网名这么多〃 提交于 2020-01-20 02:47:05
问题 In C++ if I wish to read input till the EOF I can do it in the following manner while(scanf("%d",&n)) { A[i]=n; i++; } I can then run this code as ./a.out < input.txt. What is the java equivalent of this code? 回答1: You can do this: Scanner s = new Scanner(System.in); while (s.hasNextInt()) { A[i] = s.nextInt(); i++; } 回答2: // assuming that reader is an instance of java.io.BufferedReader String line = null; while ((line = reader.readLine()) != null) { // do something with every line, one at a

execute after EOF in C

眉间皱痕 提交于 2020-01-16 18:34:51
问题 I'm doing homework for my C programming class. The question states "Write a program which reads input as a stream of characters until encountering EOF". I'm using Xcode on my macbook and the only way I know to make the program encounter EOF is using ctrl + D or ctrl + Z. But it will exit my program completely. For example I have this code: int main() { int ch; while ((ch = getchar()) != EOF) { putchar(ch); } printf("%d",ch); return 0; } Is there away for the code to execute the printf("%d",ch

mongorestore failed: restore error: error applying oplog: applyOps: EOF

霸气de小男生 提交于 2020-01-16 01:47:08
问题 I created mongodb dump with next command: mongodump /host:%MONGODB_HOST% /authenticationDatabase:admin /username:username /password:password /oplog After that I'm trying to restore dump with a command: mongorestore /noIndexRestore /oplogReplay /numParallelCollections:1 dump It restores all db and fails to replay oplog: Failed: restore error: error applying oplog: applyOps: EOF local mongoDB version: 3.0.2 (Windows 7) remote mongoDB version: 2.6.7 (Windows 8) 回答1: This is a bug with the Go

stdin allows to read with the EOF flag set

萝らか妹 提交于 2020-01-15 08:53:12
问题 On my platform, the following code allows me to successfully read from stdin even if its end-of-file flag is set, which also remains set after the read. To reproduce the behavior, first type the end-of-file shortcut ( Ctrl + D on Unix, Ctrl + Z on Windows) and then type a normal character. #include <stdio.h> // first type the shortcut for EOF, then a single character int main(void) { getchar(); printf("feof(stdin): %s\n", feof(stdin) ? "true" : "false"); int ch = getchar(); if (ch == EOF)

c++ reading undefined number of lines with eof()

五迷三道 提交于 2020-01-14 06:53:20
问题 I'm dealing with a problem using eof(). using string name; int number, n=0; while(!in.eof()) { in >> name >> number; //part of code that puts into object array n++; } sounds normal to me as it whenever there are no more text in the file. But what I get is n being 4200317. When I view the array entries, I see the first ones ats the ones in the file and other being 0s. What could be the problem and how should I solve it? Maybe there's an alternative to this reading problem (having undefined