c - What's wrong with my code? (Printing File names inside an Archive File) -
i'm trying write program open archive file unix, read files in , print files inside archive , filename. below code -- compiles, got weird output in terminal -- e.g. ?;?u?. should display 2 txt file names. can take @ code , give me guidance on i'm missing? thank you!
edit: made changes code, it's still not working. suggestion or appreciated.
#include <sys/types.h> #include <sys/stat.h> #include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <fcntl.h> #include <errno.h> #include <sys/utsname.h> #include <ctype.h> #include <string.h> #include <ar.h> int main (int argc, char **argv) { int input_fd; //char buffer[25]; struct ar_hdr my_ar; if (argc != 2) { printf("error", argv[0]); exit(exit_failure); } //open archive file (e.g., hw.a) input_fd = open(argv[1], o_rdonly); if (input_fd == -1) { perror("can't open input file\n"); exit(-1); } while (read(input_fd, my_ar.ar_name, sizeof(buffer)) > 0) { printf("%s\n", my_ar.ar_name); } close(input_fd); return 0; }
i don't see anywhere defining my_hdr is, yet printing 1 of supposed members here:
printf("%s\n", my_ar.ar_name);
you never set ar_name either.
i see read data variable named buffer, never copy buffer ar_name assuming intent.
while (read(input_fd, buffer, sizeof(buffer)) > 0) { printf("%s\n", my_ar.ar_name); }
Comments
Post a Comment