Hey everyone! I have been working on this project for about a week now. It reads in a text file and outputs how many times the top five characters are used, what the percentages are for the top five compared to the rest of the ASCII characters, and shows the characters that weren't used. I have my data file loaded in. I did a debug and it showed 2 warnings but was successful. When i run it, it says "Error when reading the file you specified." I'm sure it's line
18, but I've tried different variations with no luck. Maybe yall can give me some guidance. Thanks!

#include <iostream>
#include <fstream>

int analyzeData(FILE * f, int* result); //function declarations
void printResult( int* result, int total );

int main(int argc, char* argv[])
	char* fileName;
		fileName = "data1.txt";

	int* stat = new int[94];
	for(int i =0;i<94;i++) stat[i]=0;
	int total = 0;
	FILE * datafile = fopen( fileName, "rb");//this line is giving me the problem i believe.
	if( datafile )
		total = analyzeData( datafile, stat);
		printResult( stat, total );
		return 0;
		printf("Error when reading the file you specified.");
		return -1;

int analyzeData(FILE * f, int* result)// function definitions
	int readed = 0, total = 0;
	char* data = new char[4096];
	while( (readed = fread(data,1,1024,f)) > 0)// it says this one is a problem too, but i think it will be fine. 
		for(int i=0; i<readed;i++ )
			if(data[i]>32 && data[i]<127)
	return total;

void printResult(int* result, int total )
	int max=0;
	char maxChar = 0;
	printf("The five most common characters were:\n");
	for( int top=0;top<5;top++,max=0)
		for(char i = 0; i< 94; i++)
				max = result[i];
				maxChar = i;
	printf("\nCharacters not found were: ");
	for(char i = 0; i< 94; i++)
			printf("%c ",i+33);

Attached below are the build errors.

------ Build started: Project: Analyze, Configuration: Debug Win32 ------
c:\documents and settings\seale\my documents\visual studio 2005\projects\analyze\dataanalyize.cpp(18) : warning C4996: 'fopen' was declared deprecated
c:\program files\microsoft visual studio 8\vc\include\stdio.h(234) : see declaration of 'fopen'
Message: 'This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_DEPRECATE. See online help for details.'
c:\documents and settings\seale\my documents\visual studio 2005\projects\analyze\dataanalyize.cpp(37) : warning C4267: '=' : conversion from 'size_t' to 'int', possible loss of data
Embedding manifest...
Build log was saved at "file://c:\Documents and Settings\Seale\My Documents\Visual Studio 2005\Projects\Analyze\Debug\BuildLog.htm"
Analyze - 0 error(s), 2 warning(s)
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========

>>'fopen' was declared deprecated
That's only a microsoft warning. You can just ignore it because the c++ standards has not made it deprecated.

FILE, fopen() and all associated functions are declared in stdio.h, not iostream or fstream. In c++ programs you really should not be using those C functions, in otherwords, the file is not in the directory you think it should be in.

The problem is NOT with fopen() -- looks like you coded it correctly. I suspect you are not specifying the correct path to the file.

There is a pretty easy way to tackle your program. There are at most 255 possible characters, and less than half can be typed from the keyboard. So if you make an array of size 255 then when a character is read you can simply use that character as an index into the array and increment it.

int counts[255] = {0};

char line[] = "Hello World";
// count the number of characters in this string
for(int i = 0; line[i] != 0; i++)

Now when the above loop finished, the array counts will contain a list of the number of times each character appears in the string. You can do this for an entire file if you wish.

To find out the characters that appeared in the text just check the array counts for non-zero elements. The letter 'H' has an ascii value of 72 (look it up in any ascii chart), so counts[72] will contain the number of times 'H' appeared in the text.

Thank you for your enlightenment. It helped very much. When I read what you said I went right back to it and fixed it. It wasn't looking in the right place for it. I don't know how i could've missed something like that. I guess sometimes you miss the forest because of the trees! :-)

This question has already been answered. Start a new discussion instead.