Questions tagged [seekg]

89 questions
26
votes
1 answer

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

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…
raphnguyen
  • 3,565
  • 18
  • 56
  • 74
25
votes
2 answers

What's wrong with the ifstream seekg

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 <<…
veda
  • 6,416
  • 15
  • 58
  • 78
16
votes
1 answer

How to check whether ifstream is end of file in C++

I need to read all blocks of one large file(about 10GB) sequentially, the file contains many floats with a few strings, like this(each item splited by '\n'): 6.292611 -1.078219E-266 -2.305673E+065 sod;eiwo 4.899747e-237 1.673940e+089 -4.515213 I…
user1024
  • 982
  • 4
  • 13
  • 26
13
votes
4 answers

How to read a growing text file in C++?

I am trying to read from a file which is growing (something similar to what tail -F does), but there must be some problems with my code: string log, logFile("test.log"); size_t p = 0; while(true) { ifstream ifs(logFile.c_str()); …
Pietro
  • 12,086
  • 26
  • 100
  • 193
10
votes
1 answer

Is using istream::seekg too much expensive?

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…
Brian
  • 3,453
  • 2
  • 27
  • 39
10
votes
3 answers

seekg cannot handle file of 4294967295 bytes properly

I found that in VS2010, seekg function does not work properly when file of exactly 4294967295 bytes is opened. I'm using simple code: #include #include using namespace std; int _tmain(int argc, _TCHAR* argv[]) { …
Rusty
  • 143
  • 1
  • 7
9
votes
1 answer

Why seekg does not work with getline?

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…
rluks
  • 2,762
  • 8
  • 38
  • 56
8
votes
2 answers

fstream - How to seekg() to position x from end

I'm looking for a way to set my get pointer at position x from the end of an fstream. I tried file.seekg(-x, ios_base::end); But according to this question, this line is undefined behavior. How can I, in any way, seek to position x from the end of…
user2962533
  • 397
  • 1
  • 5
  • 18
7
votes
1 answer

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

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…
Garret Wilson
  • 18,219
  • 30
  • 144
  • 272
7
votes
3 answers

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

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…
tp77
  • 131
  • 2
  • 2
  • 4
7
votes
1 answer

How does seekg find the end of the file?

In a scenario using seekg & tellg on a file, I was wondering what is happening under the hood? // Open file and get file size int myFileSize; std::fstream myFile; myFile.open(myFileName, std::ios::in|std::ios::binary); if…
Lakey
  • 1,948
  • 2
  • 17
  • 28
5
votes
1 answer

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

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.
ATemp
  • 319
  • 3
  • 10
4
votes
1 answer

why does a blank line occupy to two bytes?

I'm new to C++. I'd like to count the blank lines at the end of a text file. But now i meet a problem. The contents of the text file are like: test.txt 1 2 blank line The code is: #include #include using namespace std; int…
Oliver Yu
  • 99
  • 5
4
votes
3 answers

C++ slow read/seekg

In my program I read in a file (here only a test file of about 200k data points afterwards there will be millions.) Now what I do is: for (int i=0;i
magu_
  • 4,766
  • 3
  • 45
  • 79
3
votes
0 answers

Extraction operator causing my program to exit?

I'm a usual lurker but this is my first post! I understand you guys like detail so I will do my best. I will appreciate whatever input anyone has. I am working on an overloading the extraction operator for an object with a dynamic array of digits.…
spanishgum
  • 1,030
  • 1
  • 14
  • 29
1
2 3 4 5 6