Disclaimer: This is for an appointment. I do not ask for an explicit code. Rather, I only ask for enough help to understand my problem and fix it myself.
I am trying to recreate the Unix utility araccording to my homework. Most of this job relates to the IO file in C, while other parts relate to system calls, etc.
In this case, I intend to create a simple list of all files in the archive. As you may have noticed, I did not go that far. The plan is relatively simple: read each file header from the archive file and print only the value stored in ar_hdr.ar_name. The remaining fields will be skipped through fseek(), including the file data, until another file is reached, after which the process will start again. If EOF is reached, the function simply ends.
I have little experience with the IO file, so I am already at a disadvantage with this appointment. I did my best to explore the right ways to achieve my goals, and I believe that I have implemented them to the best of my ability. However, in my implementation, something seems to be wrong. Data from the archive file does not seem to be read, or at least not stored as a variable. Here is my code:
struct ar_hdr
{
char ar_name[16];
char ar_date[12];
char ar_uid[6];
char ar_gid[6];
char ar_mode[8];
char ar_size[10];
};
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))
{
printf("End of file reached\n");
}
else
{
printf("Error: feed interrupted unexpectedly\n");
}
fclose(stream);
}
At this point, I only want to read the data correctly. I will work on finding the next file after this is completed. However, I would like to confirm my point of view that I am not asking for an explicit code - I need to study this material, and if someone provides me with a working code, it will not.