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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T07:49:57+00:00 2026-05-29T07:49:57+00:00

I’m having amazing difficulties using the fread and fwrite functions in C++. The project

  • 0

I’m having amazing difficulties using the fread and fwrite functions in C++.

The project is writing a rudimentary FAT16 file system and we are restricted to using fread and fwrite.

When I am initially writing the file my code looks like this:

        directoryTable = (directoryEntry *)malloc(clusterSize);

    for (int i = 0; i < bootRecord[0] / 128 ; ++i){

        directoryEntry * newEntry = (directoryEntry *)malloc(sizeof(directoryEntry));
        newEntry->name = (char *)malloc(112);
        newEntry->name[0] = 'a'
        write(1, &(newEntry->name[1]), 1);
        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;
    }

When I’m assigning the first character of directoryEntry->name to ‘a’, my intent is actually to assign it the value 0x00 so I can check later if it’s null. I simply am using a right for debugging purposes. My problem is when I read it, I get nothing back.

And when I’m reading my code looks like this:

        fseek(fp, clusterSize * root, SEEK_SET);

    for(int i = 0; i < clusterSize / 128; ++i){
        directoryEntry * newEntry = (directoryEntry *) malloc(128);
        newEntry->name = (char *) malloc(112);
        fread(newEntry->name, 112, 1, fp);
        write(1, newEntry->name[0],1);
        fread(&newEntry->size, sizeof(int), 1, fp);
        fread(&newEntry->type, sizeof(int), 1, fp);
        fread(&newEntry->creation, sizeof(int), 1, fp);
        fread(&newEntry->index, sizeof(int), 1, fp);
        directoryTable[i] = *newEntry;
    }

It should be noted that the values of clusterSize and root are also read in using similar methods. I’ve already checked and their values are accurate in both situations. Since I was able to read those in without a problem, I have no idea why I’m having such a big problem now. I feel my use of malloc is not quite right, I’ve never worked with it before.

Also, here is a definition of directoryTable if needed:

     typedef struct{
char * name;
unsigned int index;
unsigned int size;
unsigned int type;
unsigned int creation;
    } directoryEntry;

Thank you guys for you time and if you need me to clarify on anything I’d be happy to.

  • 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-29T07:49:58+00:00Added an answer on May 29, 2026 at 7:49 am

    I see a lot of little problems:

    1. You do not zero out the memory you malloc’d for name. Try memset(newentry->name, 0, 112);
    2. You are writing the second element of the name array when you write it (index 1), and the first element of the name array when you read it (index 0): write(1, &(newEntry->name[1]), 1); vs. write(1, newEntry->name[0],1);
    3. Additionally in the first write you are taking the address of it, and in the second write you are not, so it looks likes the second one should read: write(1, &(newEntry->name[0]),1);
    4. The call to fseek looks correct, but as a reader of the question I have no way of knowing if clusterSize * root is the correct offset into the file you are reading from. You also do not seek to that offset when you start writing the file (or at least don’t show it), so it becomes murkier.
    5. What exactly does the 128 you keep dividing by in your for loops represent? Also in one spot you divide clusterSize by it and in another bootrecord[0]. I assume those are the same size, but…
    6. In the read loop you malloc 128 bytes of memory, but in the write loop you malloc (sizeof(directoryEntry). Those are two different sizes, and malloc’ing the 128 bytes is wrong, since the structure contains a pointer to the 112 bytes, and not the actual 112 bytes for name.

    I am not sure if any of these are causing the problem you are seeing, which you don’t actually specify by the way, but it may at least get you pointed in the right direction.

    • 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
We're building an app, our first using Rails 3, and we're having to build
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
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 have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I want use html5's new tag to play a wav file (currently only supported
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
In my XML file chapters tag has more chapter tag.i need to display chapters

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.