简体   繁体   中英

Using fwrite() and fread() with malloc and realloc

I'm having trouble with my project for a class. I'll say right off the bat that I'm not asking for it to be done for me , I just want more clarification on how to use these functions. It appears that I am using them the right way because it will work up to the addition of 16 elements, but then it hangs on fclose() or segfaults.

Here's what I'm working with. It's a FAT16 file system:

directoryTable is an "array" of structs.

void writeDirectory(int FATTable[], directoryEntry* directoryTable)
{   
    int currentCluster = 1;
    directoryEntry* currentWrite = directoryTable;

    FILE* theFile = fopen (fileSystemName, "rb+"); 
    if(theFile != NULL)
    {
        cout << fseek (theFile, (clusterSize * currentCluster), SEEK_SET) << endl;
        cout << fwrite(currentWrite, clusterSize, 1, theFile) << endl ;

        while(FATTable[currentCluster] != 0xFFFF)
        {
            currentWrite = currentWrite + numberOfEntries;
            currentCluster = FATTable[currentCluster];
            cout << fseek (theFile, (clusterSize * currentCluster), SEEK_SET) << endl;
            cout << fwrite(currentWrite, clusterSize, 1, theFile) << endl; 
        }

        fflush(theFile);
        cout << "Closing.." << errno << endl;
        fclose(theFile);
    }
    else
        cout << "FILE COULDN'T OPEN." << endl; 

    //Clean up that pointer
    free(directoryTable);
}

Basically a pointer(currentWrite) starts at the beginning of another pointer(directoryTable) and writes a clusterSize of bytes one at a time to parts of the file. If there is more of the array/pointer(directoryTable) left over, it will increment the currentWrite up clusterSize bytes and write again.

Then it will read and build the array/pointer using this:

directoryEntry* readDirectory(int FATTable[])
{   
    int currentCluster = directoryIndex; 

    //Allocate a clusterSize of memory
    directoryEntry* directoryTable;
    directoryTable = (directoryEntry*) malloc(clusterSize);

    if(directoryTable == NULL)
        cout << "!!! ERROR: Not enough memory!" << endl;

    //A pointer to a part of that array
    directoryEntry* currentRead = directoryTable;
    numberOfDirTables = 1;

    FILE* theFile = fopen (fileSystemName, "rb+"); 
    if(theFile != NULL)
    {
        //Seek to a particular cluster in the file (
        cout << fseek (theFile, (clusterSize * currentCluster), SEEK_SET) << endl;
        cout << fread(currentRead, clusterSize, 1, theFile) << endl;

        while(FATTable[currentCluster] != 0xFFFF)
        {
            numberOfDirTables++; 
            currentCluster = FATTable[currentCluster];
            directoryTable = (directoryEntry*) realloc(directoryTable, (clusterSize*numberOfDirTables));
            if(directoryTable == NULL)
                cout << "!!! ERROR: Not enough memory!" << endl;
            currentRead = currentRead + numberOfEntries;
            cout << fseek (theFile, (clusterSize * currentCluster), SEEK_SET) << endl;

            cout << fread(currentRead, clusterSize, 1, theFile) << endl;
        }
        cout << "Closing..." << errno << endl;
        fclose(theFile);
        cout << "Closed." << endl;
    }
    else
        cout << "FILE COULDN'T OPEN." << endl; 
    return directoryTable;
}

Basically it will read from the first cluster and put that into an array. Then it will increment currentRead clusterSize amount of bytes and read from another cluster. But it realloc's first so the array is expanded another clusterSize bytes.

So my question is, am I using fwrite, fread, malloc and realloc correctly? It would appear so since it works up until some point. But I'm not too strong in C++ so I'm thinking I'm missing a small thing that is causing a major memory issue.

Also, should I be using calloc instead since I'm building an array of structs?

When you realloc() directoryTable it (probably) gets a different memory space (address) than it had before. So, after the realloc, where does currentRead point? (Leading question..)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM