Archived

This topic is now archived and is closed to further replies.

Ifstream seekg()

This topic is 5505 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

I wrote a small function to get the number of characters which I use in a file:

int GetCharacterCount(std::ifstream& File, char Character)
{
using namespace std;
char ch = NULL;
int Repetitions = 0;

while (!File.eof())
{
File.get(ch);
if (ch == Character)
Repetitions++;
}

File.seekg(0, ios::beg);

assert(!File.eof());

return Repetitions;
}

the problem is that File.seekg(0, ios::beg) isn''t setting the file pointer back to the start of the file. How can I fix this? the file is in text, not binary, so im not sure if thats the problem..

noone knows?

Share on other sites
File.seekg(0, ios::beg);

i´m sure it does set it to the beginning.try a:
#pragma pack(1) at the beginning of the file.did it for me.

Share on other sites
Before you call seekg(), call clear() to reset the stream to a working state.

Qui fut tout, et qui ne fut rien

Share on other sites
Will try that soon as I get home from work

Share on other sites
file.seekg( 0, ios::beg );
file.seekg( 0, ios::end );

int dis = file.tellg();

file.seekg( 0, ios::beg );