Original Post
My program uses a large table, but rather then recalculate it every time, I have been storing it on the hard drive. The following code is responsible for determining whether the file already exists, and if not, recalculating the table and then storing it. However, it isn't working, and I'm not sure why Specifically, fread returns 1832265664 like it is supposed to. However, the file that is created is only 1832263680 bytes big for some reason. (1984 bytes less then it is supposed to be) Therefore, fread fails and the table is recalculated every time the program executes, which isn't a desirable situation. Does anyone know what is going on? I've got 242 GB of free space on that drive, so that isn't the problem. Here are the relevant portions of the code
//unrelated functions and code omitted
int main()
{
g5table = new (nothrow) unsigned char[2*NUMCHARSFFTH];
if (g5table == NULL)
{
cout << "Error: Unable to allocate enough memory.\n";
cin >> tryloops;
return 0;
}
cout << "Memory successfully allocated\n";
loadg5table();
}
void loadg5table()
{
bool done = false;
int starttime;
int debug=-99;
FILE * datafile;
starttime = (int) clock();
cout << "Opening prefix table...\n";
datafile = fopen("g5table.dat","rb");
if (datafile == NULL)
{
cout << "Data table file could not be opened.\n";
}
else
{
debug=fread( g5table, sizeof(unsigned char), 2*NUMCHARSFFTH, datafile);
cout << debug << "/" << 2*NUMCHARSFFTH << " values read from file\n";
//if (fread( g5table, sizeof(unsigned char), 2*NUMCHARSFFTH, datafile) != 2*NUMCHARSFFTH)
if (debug != 2*NUMCHARSFFTH)
{
cout << "Data table file could not be read.\n";
}
else
{
cout << "Prefix table sucessfully read from file." << (float) (((int) clock())-starttime)/CLOCKS_PER_SEC<< " seconds elapsed\n";
done=true;
}
fclose( datafile );
}
if (!done)
{
cout << "Generating prefix table...\n";
createg5table();
cout << "Prefix table created sucessfully. " << (float) (((int) clock())-starttime)/CLOCKS_PER_SEC<< " seconds elapsed\n";
datafile = fopen("g5table.dat","wb");
cout << fwrite( g5table, sizeof(unsigned char), 2*NUMCHARSFFTH, datafile ) <<" values written\n";
cout << "Table stored to file for future use.\n";
}
}