Skip to main content
GameDev.net gamedev.net
🔒 Locked

file reading and writing issues

Started by Storyyeller Apr 4, 2009 at 4:50 PM 1 replies 1k views
Original Post
Storyyeller
Storyyeller
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";
    }
}


I trust exceptions about as far as I can throw them.
fastcall22
fastcall22
Try calling fflush before the closing the file.
Storyyeller
Storyyeller
I did that and it worked perfectly. Thanks for the help
I trust exceptions about as far as I can throw them.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.