Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

File IO does not appear to be reading correctly

Tags:

c

io

unix-ar

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.

like image 817
idigyourpast Avatar asked Oct 21 '22 18:10

idigyourpast


1 Answers

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.

like image 90
Lawrence Dol Avatar answered Oct 24 '22 11:10

Lawrence Dol