ifstream

No `while (!my_ifstream.eof()) { getline(my_ifstream, line) }` in C++?

杀马特。学长 韩版系。学妹 提交于 2019-12-10 15:39:02
问题 On this website, someone writes: while (! myfile.eof() ) { getline (myfile,line); cout << line << endl; } This is wrong, read carefully the documentation for the eof() memberfunction. The correct code is this: while( getline( myfile, line)) cout << line << endl; Why is this? 回答1: There are two primary reasons. @Etienne has pointed out one: reading could fail for some reason other than reaching the end of the file, in which case your first version will go into an infinite loop. Even with no

C++ How Do I Read All .txt Files in a Directory?

岁酱吖の 提交于 2019-12-10 11:42:52
问题 How do I read all .txt files in a certain directory? Let's say in my C:\ I have a foo.txt and a foo2.txt . Is there a way to read both of these two without having to do something like this? string text; string text2; ifstream myFile ("foo.txt"); ifstream myFile2 ("foo2.txt"); while(myFile << text){ }; while(myFile2 << text2){ }; In other words, is it possible to put a wildcard *.txt to instruct to read all .txt files? ifstream myFile ("*.txt"); 回答1: You'd typically do this by passing the name

String is not printing without new line character in C++

試著忘記壹切 提交于 2019-12-10 11:17:04
问题 I'm opening a file, and getting lines from it. The first line should say how many variables there are, and what their names are. The second line should be a logic equation using these variables. The assignment is to have it print out a truth table for the variables and equation. The first line the program is taking in is not printing without me inserting a new line character. I tried converting to a string and using both printf and cout. Main file that inputs everything: #include "truthTable2

Is using istream::seekg too much expensive?

佐手、 提交于 2019-12-10 03:37:55
问题 In c++, how expensive is it to use the istream::seekg operation? EDIT: How much can I get away with seeking around a file and reading bytes? What about frequency versus magnitude of offset? I have a large file (4GB) that I am parsing, and I want to know if it's necessary to try to consolidate some of my seekg calls. I would assume that the magnitude of differences in file location play a role--like if you seek more than a page in memory away, it will impact performance--but small seeking is

Cannot read simple binary integers from file? (C++)

情到浓时终转凉″ 提交于 2019-12-09 16:08:18
问题 My code is simply as this: UPDATED : #include <iostream> #include <fstream> using namespace std; int main(int argc, char **argv) { ifstream r("foo.bin", ios::binary); ofstream w("foo.bin", ios::binary); int i; int ints[10] = {0,1,2,3,4,5,6,8,9}; w.write((char*)&ints, sizeof(ints)); int in_ints[10]; r.read((char*)&in_ints, sizeof(in_ints)); for(i = 0;i < 10;i++) { cout << in_ints[i] << " "; } cout << endl; return 0; } Now, the write portion appears to be successful, for example running the od

how can I read exactly 128 bytes from an fstream into a string object? [duplicate]

对着背影说爱祢 提交于 2019-12-09 10:23:23
问题 This question already has answers here : Reading directly from an std::istream into an std::string (6 answers) Closed 3 years ago . How do I read exactly 128 bytes from an fstream into a string object? I wrote some code to read the first 128 bytes of a file and print it and then the last 128 bytes of the file and print that. The last part works, since you can easily iterate to EOF, but how do I get exactly 128 bytes from the front? The code below doesn't work since you can't add 128 to an

c++ use ifstream from memory

泪湿孤枕 提交于 2019-12-08 18:56:36
问题 I have some code that uses ifstream to read some data from a file and everything works. Now I wish, without modifying some code, read this data from a memory, actually I have a char * that contains the data... How can I put my char * data into a ifstream without reading effectively the file? 回答1: If the code that uses the ifstream& could be changed slightly to use an istream& then you could easily switch between ifstream and istringstream (for reading data from memory): void read_data(std:

Ifstream open() doesn't set error bits when argument is a directory

僤鯓⒐⒋嵵緔 提交于 2019-12-08 17:28:20
问题 In a C++ program, using std::ifstream, I'm attempting to open a user-specified file -- so far so good. However, I accidentally entered a filename that's actually a directory, and I was quite surprised to see that attempting to open() that directory didn't generate any errors. Here's a minimal example: std::ifstream f; f.open(".."); if(!f.is_open() || !f.good() || f.bad() || f.fail()) { std::cout << "error bit set on open" << std::endl; return 1; } No sign of error here. If I go on and attempt

C++ UTF-16 to char conversion (Linux/Ubuntu)

主宰稳场 提交于 2019-12-08 11:13:04
问题 I am trying to help a friend with a project that was supposed to be 1H and has been now 3 days. Needless to say I feel very frustrated and angry ;-) ooooouuuu... I breath. So the program written in C++ just read a bunch of file and process them. The problem is that my program reads files which are using a UTF-16 encoding (because the files contain words written in different languages) and a simple use to ifstream just doesn't seem to work (it reads and outputs garbage). It took me a while to

Read a big file by lines in C++

耗尽温柔 提交于 2019-12-08 10:19:49
问题 I have a big file nearly 800M, and I want to read it line by line. At first I wrote my program in Python , I use linecache.getline: lines = linecache.getlines(fname) It costs about 1.2s. Now I want to transplant my program to C++. I wrote these code: std::ifstream DATA(fname); std::string line; vector<string> lines; while (std::getline(DATA, line)){ lines.push_back(line); } But it's slow(costs minutes). How to improve it? Joachim Pileborg mentioned mmap() , and on windows CreateFileMapping()