seekg

Text File Binary Search

做~自己de王妃 提交于 2019-12-06 15:33:22
I have a test file that looks like this: Ampersand Gregorina 5465874526370945 Anderson Bob 4235838387422002 Anderson Petunia 4235473838457294 Aphid Bumbellina 8392489357392473 Armstrong-Jones Mike 8238742438632892 And code that looks like this: #include <iostream> #include <string> #include <fstream> class CardSearch { protected: std::ifstream cardNumbers; public: CardSearch(std::string fileName) { cardNumbers.open(fileName, std::ios::in); if (!cardNumbers.is_open()) { std::cout << "Unable to open: " << fileName; } return; } std::string Find(std::string lastName, std::string firstName) { //

Why seekg does not work with getline?

自闭症网瘾萝莉.ら 提交于 2019-12-05 07:18:34
Seekg does not seem to work, when I reach EOF in myFile. ifstream myFile("/path/file"); for(int i; i < 10; i++){ myFile.seekg(0);//reset position in myFile while(getline(myFile, line)){ doSomething } } So, now I am opening input stream every loop: for(int i; i < 10; i++){ ifstream myFile("/path/file");//reset position in myFile while(getline(myFile, line)){ doSomething } } But I would rather seek to position 0. How can I achieve that? Joseph Mansfield Make sure you clear the error flags before the call to myFile.seekg() : myFile.clear(); After the EOF flag has ben set, you will not be able to

How do I implement seekg() for a custom istream/streambuf?

寵の児 提交于 2019-12-05 06:04:30
I used to be a C++ expert a decade ago, but for the past 10 years I've been programming Java. I just started a C++ project that uses a small third-party XML parser. The XML parser accepts an STL istream. My XML data is coming from a Windows COM IStream. I thought I'd do the Right Thing and create an adapter to take the IStream data and present it to the XML parser through an istream. I followed the excellent tutorial at http://www.mr-edd.co.uk/blog/beginners_guide_streambuf and created a COMStreambuf that takes data from the underlying COM IStream, and used it as a buffer for a custom

Is using istream::seekg too much expensive?

余生颓废 提交于 2019-12-05 03:35:45
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 of no consequence. Is this correct? This question is heavily dependent on your operating system and disk

seekg() failing mysteriously

五迷三道 提交于 2019-12-04 21:38:44
I have a 2884765579 bytes file. This is double checked with this function, that returns that number: size_t GetSize() { const size_t current_position = mFile.tellg(); mFile.seekg(0, std::ios::end); const size_t ret = mFile.tellg(); mFile.seekg(current_position); return ret; } I then do: mFile.seekg(pos, std::ios::beg); // pos = 2883426827, which is < than the file size, 2884765579 This sets the failbit. errno is not changed. What steps can I take to troubleshoot this? I am absolutely sure that: The file size is really 2884765579 pos is really 2884765579 The failbit is not set before .seekg()

What's wrong with the ifstream seekg

回眸只為那壹抹淺笑 提交于 2019-12-03 22:57:32
I am trying to do a seek and re-read the data. but the code fails. The code is std::ifstream ifs (filename.c_str(), std::ifstream::in | std::ifstream::binary); std::streampos pos = ifs.tellg(); std::cout <<" Current pos: " << pos << std::endl; // read the string std::string str; ifs >> str; std::cout << "str: " << str << std::endl; std::cout <<" Current pos: " <<ifs.tellg() << std::endl; // seek to the old position ifs.seekg(pos); std::cout <<" Current pos: " <<ifs.tellg() << std::endl; // re-read the string std::string str2; ifs >> str2; std::cout << "str2: (" << str2.size() << ") " << str2 <

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

一世执手 提交于 2019-12-01 20:30:58
问题 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

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 &);

seekg() function fails

ぃ、小莉子 提交于 2019-11-29 15:46:59
I am trying to write some simple code which will read a text file but reads the first line twice. I thought this would be as simple as something like this std::ifstream file; file.open("filename", std::ios_base::in); std::string line; std::getline(file, line); // process line file.seekg(0, ios::beg); while (std::getline(file, line)) { // process line } However the seekg must fail as the first line is not processed twice. Any idea why? PLEASE NOTE: This is not the problem I am faced with but a simplified version of it so as not to have to paste multiple classes code and multiple functions. The

Using seekg() when taking input from redirected stdin

我与影子孤独终老i 提交于 2019-11-28 14:20:13
So i'm trying to read in a string of characters twice using cin.get(). The input is being redirected as "program < input". So it is valid to use seekg(). As the titel says, I thought I would be able to use seekg() to save the beginning position of the string, so I could come back to use the starting position of the same string again. Here is my attempt: char c; while (cin.get(c)) { //do stuff } cin.seekg(0, ios::beg); while (cin.get(c)) { //do stuff with the string a second time } The second while loop isn't doing anything, so I'm obviously not using seekg correctly. Could someone tell me what