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 7421221
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T08:22:20+00:00 2026-05-29T08:22:20+00:00

I’m working to create a rudimentary file system in c++ and am having issues

  • 0

I’m working to create a rudimentary file system in c++ and am having issues assigning names to files in my directory table. Here is the definition of a directoryEntry struct.

typedef struct{
   char name[112];
   unsigned int index;
   unsigned int size;
   unsigned int type;
   unsigned int creation;
} directoryEntry;

Here is some code where I create a new blank file.

if(canFit){
    for ( int i = 0; i < numClusters; ++i){

        if (directoryTable[i].name[0] == 0x00){
            //directoryTable[i].name = *newFile;
            strcpy(directoryTable[i].name, newFile);
            printf("%s   %d   ", directoryTable[i].name, i);
            directoryTable[i].index = location;
            directoryTable[i].size = 0;
            directoryTable[i].type = 0x0000;
            directoryTable[i].creation = time(NULL);
            return 0;
        }
    }

What is happening here is I’m scanning the directory table for entries that contain a null byte as the first character of the filename. This tells me that that particular directory entry is not occupied. Then I use strcpy to assign the newFile (which is declared as a parameter as char newFile[112]).

The problem is the directory name prints properly in the above printf (printf(“%s %d “, directoryTable[i].name, i);) but it appears that the data contained in directoryTable[i].name is deleted. The above code is contained in a method called touch. When the code executes I get something like the following:

touch(“file1”);
file1 0

touch(“file2);
file2 0

which means that the value associated to index 0 is temporarily changed to file1, but is then changed back to several null bytes (Which is how it is originally allocated.) So my question is, why doesn’t the value of directoryTable[i] stay the same through multiple calls to touch? directoryTable is a global variable so if the value is assigned it shouldn’t disappear when out of scope.

It should also be noted that directoryTable is defined as directoryEntry* directoryTable if that has any bearing on my problem.

My professor will not allow us to use most of the standard libraries in C++. Strings, for example, are illegal. So if you are wondering, that is why I’m not using strings.

Thank you for your time.

EDIT:

Here is code where the directoryEntrys that make up the directoryTable are allocated and added to the directoryTable. They are also added to a file but I feel that is irrelevant to this particular question:

    fseek(fp, clusterSize * root, SEEK_SET);

    for (int i = 0; i < bootRecord[0] / 128 ; ++i){
        fseek(fp, clusterSize * root + 128 * i, SEEK_SET);

        directoryEntry * newEntry = (directoryEntry *)malloc(sizeof(directoryEntry));
        //newEntry->name = (char *)malloc(112);
        //memset(newEntry->name, 0x00, 112);
        newEntry->size = 0;
        newEntry->type = 0;
        newEntry->creation = 0x0000;
        newEntry->index = 0;

        fwrite(newEntry->name, 112, 1, fp);
        fwrite(&newEntry->size, sizeof(int), 1, fp);
        fwrite(&newEntry->type, sizeof(int), 1, fp);
        fwrite(&newEntry->creation, sizeof(int), 1, fp);
        fwrite(&newEntry->index, sizeof(int), 1, fp);

        directoryTable[i] = *newEntry;
    }
  • 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-29T08:22:20+00:00Added an answer on May 29, 2026 at 8:22 am

    You’ll need some sort of actual storage defined for your directory table:

    #define NUM_FILES 15 
    DirectoryEntry directoryTable[NUM_FILES]
    

    The pointer you’ve defined just points to random memory. I’m surprised it doesn’t segfault.

    Edit

    You malloc the DirectoryEntries, but you’re not storing them anywhere valid. You have no storage for your pointers, so you’re losing them as soon as you assign them to the directoryTable You’ll either need to create an array of DirectoryEntry*:

    DirectoryEntry* directoryTable[MAX_ENTRIES]
    

    Where you can put your malloc’d pointers or, just create the whole table at runtime:

    DirectoryEntry* directoryTable;
    
    int newFS(int size, int clusterSize) {
        //Use C++ 'new' instead of malloc
        directoryTable = new DirectoryEntry[size / clusterSize];
    
        //Make sure to 'delete directoryTable' at program completion.
    }
    
    • 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'm working with an upstream system that sometimes sends me text destined for HTML/XML
I have just tried to save a simple *.rtf file with some websites and
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported
In my XML file chapters tag has more chapter tag.i need to display chapters
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build

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.