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.

You need to use fin.seekg(0) or fin.seekg(0,ios::beg) to get the effect you are (ahem) seeking.

Your technique is somewhat inefficient. More efficient to read blocks into memory, then count white space and newlines to get number of 'words' and 'lines'. If the blocks are obtained by a call to getline() the line count is more or less done for you.

You need to use fin.seekg(0) or fin.seekg(0,ios::beg); to get the effect you are (ahem) seeking.

Your technique is somewhat inefficient. More efficient to read blocks into memory, then count white space and newlines to get number of 'words' and 'lines'. If the blocks are obtained by a call to getline() the line count is more or less done for you.

Unfortunately, that didn't work either.

I understand getline, but may I ask how it is more efficient in the case of words? Wouldn't I still have to parse through each character in search of whitespace to determine a word? Could you give me a quick example, because I don't feel like I am envisioning this the way it's actually supposed to work.

Check out http://www.cplusplus.com/reference/iostream/istream/seekg/ seekg.

Do you have to make 3 separate functions? You could definitely do it all in one pass.

I know I could, I just wanted to break it all off into 3 separate ones so I could pull them out for quicker use in some other programs I will have to write later.

I know I could, I just wanted to break it all off into 3 separate ones so I could pull them out for quicker use in some other programs I will have to write later.

Well, that's a very sensible answer.

I think the getline buys you the ease of having everything in a string in addition to making the line numbering slightly less painful.

Unfortunately, that didn't work either.

I understand getline, but may I ask how it is more efficient in the case of words? Wouldn't I still have to parse through each character in search of whitespace to determine a word? Could you give me a quick example, because I don't feel like I am envisioning this the way it's actually supposed to work.

But using getline() would atleast make your line count function easier. And I think that's what griswolf meant ...

But using getline() would atleast make your line count function easier. And I think that's what griswolf meant ...

Exactly. Also, if you only parse the file once, the whole seek issue goes away. Another way to get what OP wants later (good to plan ahead!) is to create a single utility, which if run from the command line might be spelled wc ;) that returns a triple (or a pointer to struct WCresults{int lines; int words; int characters) ) You then dig out of that triple whatever data you are interested in.

edit: Here's wc code: http://www.gnu.org/software/cflow/manual/html_node/Source-of-wc-command.html

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.