Checking the occurrence of characters from binary file

The main program asks for user input of what ascii decimal value they wish to know the occurrence of in the file.
The problem I'm having is that it seems to ignore special characters and the values required for input are 0, 40, 255 - which are all special characters. So obviously I'm doing something wrong. I just can't figure out where the problem is.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <string>
#include "ReadFileAsString.h"
#include "asciiwhat.h"

using namespace std;

int main ()
{
	what ascwhat;
	int query;

	cout << "What number is you querying?" << endl;
	cin >> query;
	cout << endl;
	ascwhat.numOccurrence(query);

	system ("Pause");
	return 0;
}

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
#include <iostream>
#include "asciiwhat.h"
#include "ReadFileAsString.h"

using namespace std;


void what::numOccurrence(int query)
{
	int ascii[256];
	int i;

	ReadFileAsString MyFile("C:\\Users\\Cadaver\\Documents\\TCC\\C++\\Assignments\\Assignment4 Files\\Assignment4TestFile.bin");

	for (i = 0; i < 256; i++)
		ascii[i] = 0;

	int c = MyFile[0];
	for(i = 0; i < c; i++)
		ascii[MyFile[i]]++;


	for(i = 0; i < 256; i++)
	{
		if (i == query)
		{
			cout << "Woh, guess what?" <<endl;
			cout << query << " occurs " << ascii[i] << " times." << endl << endl;
			cout << "That's a " << (char) i << endl;
		}
	}
}
You don't show the code where (a) the file is opened and (b) you find its length.

This little block of code seems central:
18
19
20
	int c = MyFile[0];
	for(i = 0; i < c; i++)
		ascii[MyFile[i]]++;


On line 20 it looks as though MyFile[0] is just one character in the file. But line 18 suggests it is the length of the entire file. Needs some clarification here.
Topic archived. No new replies allowed.