2

I'm using C++ to read some chars from a file and store them in a buffer, however, I'm witnessing strange behavior with ifstream's read function.

To start with, I'm using this code snippet to get the file's length:

input.seekg (0, input.end);
int length = input.tellg();
input.seekg (0, input.beg);

After that, I call read() to get length bytes from the file.

It works fine, except for one thing: If I use input.gcount() to see how many bytes were read, this number is much less that the length of the file we got above (but shows the actual nuber of bytes in the file).

Do you guys know anything about the difference between the file's length, found by using tellg(), and the number of bytes read afterwards, as reported by gcount()?

Sorry for any formatting issues (I'm using my phone).

Thanks a lot.

Edit :

That's the code (more or less) I'm using:

ifstream input("test.txt");

input.seekg (0, input.end);
int length = input.tellg();
input.seekg (0, input.beg);

input.read(buffer,length);

int extracted = input.gcount();
Edward
  • 5,942
  • 4
  • 38
  • 55
user2455103
  • 505
  • 2
  • 4
  • 13
  • `tellg()` returns the position in the external sequence while `gcount()` returns the number of characters read from the most recent unformatted input function. It follows that unless you read the total number of characters from the external sequence, `gcount()` will invariably return a smaller value than the total number of characters. – David G Jan 19 '14 at 17:07
  • Thanks for your comment... Yes, I forgot to mention that, I *am* reading the whole file with read()... Shouldn't the number returned by tellg() be the same as gcount()? – user2455103 Jan 19 '14 at 17:10
  • Then please show the code. – David G Jan 19 '14 at 17:11
  • Can you add the `ifstream` instantiation code? Perhaps you are trying to read a binary file without using `std::ios::binary`, this a common source of failure. – Aleksander Bavdaz Jan 19 '14 at 17:12
  • Ahh well, as I mentioned I currently can't show the entire source , as I'm using my phone... I can edit the relevant code however. – user2455103 Jan 19 '14 at 17:13
  • I'm trying to read a txt file like this : ifstream input ("test.txt"); – user2455103 Jan 19 '14 at 17:13
  • @AleksanderBavdaz That's most likely the issue here. – David G Jan 19 '14 at 17:13
  • Can a txt file be a binary file?! – user2455103 Jan 19 '14 at 17:14
  • Extracted is different than length in the code above – user2455103 Jan 19 '14 at 17:18
  • Yes, a `.txt` file can contain anything, depending on what you wrote into it. The extension is no guarantee for the files contents. – Aleksander Bavdaz Jan 19 '14 at 17:31
  • What I really meant by .txt (wasn't really obvious), is a normal text file. In my case it contains just integers written in notepad... – user2455103 Jan 19 '14 at 17:33

1 Answers1

2

Fstream's tellg / seekg returning higher value than expected

Just found this link... It explains it nicely!

Turns out I need to search a little bit more before posting...

Thank you all for your answers

Community
  • 1
  • 1
user2455103
  • 505
  • 2
  • 4
  • 13