Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7526415
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T03:49:27+00:00 2026-05-30T03:49:27+00:00

I’m having trouble with my project for a class. I’ll say right off the

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T03:49:28+00:00Added an answer on May 30, 2026 at 3:49 am

    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..)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.