How do I appropriately go about resetting where the pointer is in a file?
I was under the impression that I had the correct way, but my code is not properly functioning. I need to do a character count, word count, line count. Which ever function I run first returns the correct result, however whatever function I run after that, it skips my while loop and does not return the correct result.
#include <fstream>
#include <iostream>
using namespace std;
int char_count(ifstream &);
int word_count(ifstream &);
int line_count(ifstream &);
int main(void)
{
ifstream fin;
fin.open("input2.txt");
cout <<"Characters: " << char_count(fin)<<endl;
cout <<"Words: " << word_count(fin)<<endl;
cout <<"Lines: " << line_count(fin)<<endl;
fin.close();
system ("PAUSE");
return 0;
}
int char_count(ifstream & fin)
{
fin.clear();
int count =0;
char x;
while (fin.get(x))
{
count++;
}
return count;
}
int word_count(ifstream & fin)
{
fin.clear();
int count =1;
bool flag = false;
char x;
while (fin.get(x))
{
if ( x == ' ' || x == '\n' || x == '\t')
{
if(!flag)
{
count++;
flag=true;
}
}
else
flag = false;
}
return count;
}
int line_count(ifstream & fin)
{
fin.clear();
int count =1;
char x;
while (fin.get(x))
{
if (x == '\n')
count++;
}
return count;
}
Thanks for any help I may receive! This has got me stumped.