Counting characters

How can I search for a single character in a text file and count how many user specified character are in the text file?
For example:
User input: 'a'
Number of a's in text file: 32

1
2
3
4
5
6
7
8
        cout<<"Please enter the file name >> ";
	cin>>fileName;

	infile.open(fileName);
	outfile.open("word2.txt");

	cout<<"Please enter a single specific character to search for: "<<endl;
	cin>>x;
1
2
3
4
5
6
char ch;
while (infile.get(ch))
{
    if (ch == x)
    { /*Increment a counter*/ }
}
I tried that but it's not counting how many times the input character appears in the file.
i think you need to select a proper destination.
it should be like.

 
outfile.open("C:/Windows/word2.txt");


make sure you have a text document in that specified location.
Topic archived. No new replies allowed.