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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T07:56:13+00:00 2026-06-18T07:56:13+00:00

Disclaimer: this is for an assignment. I am not asking for explicit code. Rather,

  • 0

Disclaimer: this is for an assignment. I am not asking for explicit code. Rather, I only ask for enough help that I may understand my problem and correct it myself.

I am attempting to recreate the Unix ar utility as per a homework assignment. The majority of this assignment deals with file IO in C, and other parts deal with system calls, etc..

In this instance, I intend to create a simple listing of all the files within the archive. I have not gotten far, as you may notice. The plan is relatively simple: read each file header from an archive file and print only the value held in ar_hdr.ar_name. The rest of the fields will be skipped over via fseek(), including the file data, until another file is reached, at which point the process begins again. If EOF is reached, the function simply terminates.

I have little experience with file IO, so I am already at a disadvantage with this assignment. I have done my best to research proper ways of achieving my goals, and I believe I have implemented them to the best of my ability. That said, there appears to be something wrong with my implementation. The data from the archive file does not seem to be read, or at least stored as a variable. Here’s my code:

struct ar_hdr
{
    char ar_name[16];   /* name */
    char ar_date[12];   /* modification time */
    char ar_uid[6];     /* user id */
    char ar_gid[6];     /* group id */
    char ar_mode[8];    /* octal file permissions */
    char ar_size[10];   /* size in bytes */
};

void table()
{
    FILE *stream;
    char str[sizeof(struct ar_hdr)];
    struct ar_hdr temp;

    stream = fopen("archive.txt", "r");
    if (stream == 0)
    {
        perror("error");
        exit(0);
    }

    while (fgets(str, sizeof(str), stream) != NULL)
    {
        fscanf(stream, "%[^\t]", temp.ar_name);
        printf("%s\n", temp.ar_name);
    }
    if (feof(stream))
    {
        // hit end of file
        printf("End of file reached\n");
    }
    else
    {
        // other error interrupted the read
        printf("Error: feed interrupted unexpectedly\n");
    }

    fclose(stream);
}

At this point, I only want to be able to read the data correctly. I will work on seeking the next file after that has been finished. I would like to reiterate my point, however, that I’m not asking for explicit code – I need to learn this stuff and having someone provide me with working code won’t do that.

  • 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-06-18T07:56:15+00:00Added an answer on June 18, 2026 at 7:56 am

    You’ve defined a char buffer named str to hold your data, but you are accessing it from a separate memory ar_hdr structure named temp. As well, you are reading binary data as a string which will break because of embedded nulls.

    You need to read as binary data and either change temp to be a pointer to str or read directly into temp using something like:

    ret=fread(&temp,sizeof(temp),1,stream);
    

    (look at the doco for fread – my C is too rusty to be sure of that). Make sure you check and use the return value.

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

Sidebar

Related Questions

Disclaimer: this is for an assignment. I am not asking for explicit code, only
Disclaimer: This is for an assignment. I am not asking for explicit code answers,
(Firstly, as a disclaimer, this is related to an assignment. I'm not asking anyone
Disclaimer: this is needed for an old and ugly codebase that I'd much rather
Disclaimer: this question may not have practical value, it's more of a puzzle/curiosity question.
Disclaimer: This is for a homework assignment, but the question is not regarding the
Disclaimer : this is a strange issue that only happens in a Kindle Fire
Disclaimer: this is not a question about how to install asp.net or an application
Disclaimer: This is for a programming class, but it is not the answer or
Disclaimer: not sure this is WordPress related or not. I'm following a simple tutorial

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.