seekg

seekg() failing mysteriously

╄→гoц情女王★ 提交于 2019-12-22 00:34:28
问题 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

Read from same file (until EOF) using ifstream after file contents change

只愿长相守 提交于 2019-12-19 10:47:24
问题 Requirement : I must read until EOF (16 bytes a time) from a particular file , and then say sleep for 5 seconds. Now, after 5 seconds, when I try to read from the file (whose contents would have been appended by that time), the intended design must be in such a way that it reads from the point where it left previously and again scan the contents (16 bytes a time) until EOF is reached. I have written (basic) code to read from the given file (until EOF - 16 bytes a time) using ifstream as

seekg() function fails

本小妞迷上赌 提交于 2019-12-18 09:06:35
问题 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

Using seekg() when taking input from redirected stdin

你离开我真会死。 提交于 2019-12-17 21:16:37
问题 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

fstream seekg(), seekp(), and write()

余生长醉 提交于 2019-12-17 18:34:34
问题 I'm looking for some clarification on how seekg() and seekp() works with respect to when you are writing to a file. Say for instance I had a file like so: offset 0: 2 offset 4: 4 offset 8: 6 offset 12: 8 offset 16: 10 Now I want to open the file and do some seeks to read and write values. fstream file; file.open("file.txt", fstream::in |fstream::out | fstream::binary); file.seekp(0, ios::end) // seek to the end of the file int eofOffset = file.tellp(); // store the offset of the end-of-file,

C++ how to check if a stream (iostream) is seekable

六眼飞鱼酱① 提交于 2019-12-14 01:36:50
问题 Is there a way I can check if an istream of ostream is seekable? I suspect doing a test seek and checking for failbit is not correct since the seek can fail for unrelated reasons. I need this to work on Linux and mac, if that makes a difference. 回答1: Iostreams doesn't give you much. Stream objects are just wrappers around a buffer object derived from class std::streambuf . (Assuming "narrow" characters.) The standard derived buffer classes are std::stringbuf for strings and std::filebuf for

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

Text File Binary Search

最后都变了- 提交于 2019-12-08 06:54:30
问题 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

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

一个人想着一个人 提交于 2019-12-07 04:07:06
问题 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

Why seekg does not work with getline?

拜拜、爱过 提交于 2019-12-07 01:56:55
问题 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? 回答1: Make sure you clear the error flags before the call