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;
if(argc>1)
fileName=argv[1];
else
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;
}
else
{
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)
{
result[data[i]-33]++;
total++;
}
}
}
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++)
{
if(result[i]>max)
{
max = result[i];
maxChar = i;
}
}
result[maxChar]=-1;
printf("%c\t%d\t%6.2f%%\n",maxChar+33,max,(100.00*max/total));
}
printf("\nCharacters not found were: ");
for(char i = 0; i< 94; i++)
{
if(result[i]==0)
{
printf("%c ",i+33);
}
}
}
Attached below are the build errors.
------ Build started: Project: Analyze, Configuration: Debug Win32 ------
Compiling...
DataAnalyize.cpp
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
Linking...
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 ==========