program that counts the number of letters in a file

Hi,

I'm trying to write a program that counts the number of letters in a file using an input file stream. Here's the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <fstream>
#include <iostream>
#include <cctype>
#include <cstdlib>

using namespace std;

//returns the sum of letters there are in the words of a file
long count_letters(ifstream& in_stream);

int main()
{
	ifstream in_stream;
	in_stream.open("test3.txt");

	long letters;
	letters = count_letters(in_stream);
	in_stream.close();
	cout << "The number of letters in the file is: " << letters << endl << endl;

	cout << "End of the program.";
	char a;
	cin >> a;

    return 0;
}

/*
long count_letters(ifstream& in_stream)
{
	bool more = true;
	char next;
	long i(0);
	while(true)
	{
		in_stream.get(next);
		cout << "hello";
		if (cin.fail())
		{
			return i;
		}
		if((!isspace(next)) && (next != '.') && (next != ','))
		{
			i++;
		}
		return 1;
	}
	return 1;
}*/


long count_letters(ifstream& in_stream)
{
	char next;
	long i(0);
	in_stream.get(next);
	//cout << next;
	while(!in_stream.fail())
	{
		if((!isspace(next)) && (next != '.') && (next != ','))
		{
			i++;
		}
		in_stream.get(next);
	}
	return i;
}


Don't mind the stuff that was "commented out." I don't know why but I think my count_letters function isn't working. it seems like it never enters the while loop. The first value that gets stored in the char variable "next" is some weird character that looks like "ll" only the second "l" has a hole in the middle of it. Someone told me it was a vertical bar character (http://en.wikipedia.org/wiki/Vertical_bar) and it seems like it doesn't matter what's in the input file (test3.txt in the above code), the function NEVER enters the while loop.

BTW, for the purpose of this program, a letter is anything that isn't a whitespace, a comma, or a period. thank you for reading this far.
Sorry. Can't help much. It worked fine for me.
To debug, check is_open() and fail() before calling count_letters, check that there is no typo in file name.
Why do you use ifstream.fail() instead of ifstream.eof()?
More importantly, what is your input file?

Also I think you can use cin.get(); to pause your program, instead of reading a variable in cin >> a;.
Catfish,

when I use !ifstream.eof() on line 58 then my program still doesn't work. My input file is a bunch of random characters saved under a text file:


ljksdf sfdlk 234m dsfdlk ../ds, sfdlk ,f,
fd, dfg,

I know the location under which it's saved isn't the problem because it's in the same location where I've been saving all my other experimental text files which I use to test C++ programs, all of which have given me no problems so far.

I noticed the character saved in next is always a vertical bar. I have no idea why this is and I'm guessing it's the root of my problems but I have no idea why next would be set to a vertical bar.
I suppose that next is initialised with '|', and your ifstream is in an invalid state.
I inserted the following lines at line 15:

if(in_stream.fail())
{
cout << "Input stream failed to open.\n";
cin.get();
exit(1);
}

and found out that my input file stream fails to open. Why would that happen? Anyone?
It could be:
_No such file or directory
_Permission denied

check errno (with perror)
ne555,

Your comment helped me out a lot. My file was in the wrong directory. Thank you.
Topic archived. No new replies allowed.