for homework assignment, wrote code in c print out permissions , file name of files stored in achive file. code compiles, reason, name of second file returned in new line.
here how output looks in terminal:
rw-r--r-- 501/20 1-s.txt rw-r--r-- 501/20 2-s.txt (empty blank line here) this how want output look:
rw-r--r-- 501/20 1-s.txt rw-r--r-- 501/20 2-s.txt (no empty blank line here) can take @ code , give me tips on how fix code? suspect it's related way read , select file name (using my_ar.ar_name), can't figure out how fix it. thank time.
#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) { file *fp; size_t readnum; long long tot_file_size, cur_file_size; struct stat filestat; struct ar_hdr my_ar; //open archive file (e.g., hw.a) fp = fopen(argv[1], "r"); if (fp == null) { perror("error opening file\n"); exit(-1); } //size of archive file fseek(fp, 0, seek_end); tot_file_size =ftell(fp); rewind(fp); //read data struct fseek(fp, strlen(armag), seek_set); file_size_cnt = 0; while (ftell(fp) < tot_file_size - 1) { readnum = fread(&my_ar, sizeof(my_ar), 1, fp); if (stat(argv[1], &filestat) == -1) { perror("stat"); exit(exit_failure); } if ((filestat.st_mode & s_ifmt) == s_ifreg) { printf( (filestat.st_mode & s_irusr) ? "r" : "-"); printf( (filestat.st_mode & s_iwusr) ? "w" : "-"); printf( (filestat.st_mode & s_ixusr) ? "x" : "-"); printf( (filestat.st_mode & s_irgrp) ? "r" : "-"); printf( (filestat.st_mode & s_iwgrp) ? "w" : "-"); printf( (filestat.st_mode & s_ixgrp) ? "x" : "-"); printf( (filestat.st_mode & s_iroth) ? "r" : "-"); printf( (filestat.st_mode & s_iwoth) ? "w" : "-"); printf( (filestat.st_mode & s_ixoth) ? "x" : "-"); printf("%.*s", 15, my_ar.ar_name); printf("\n"); } cur_file_size = atoll(my_ar.ar_size); if (fseek(fp, cur_file_size, seek_cur) != 0) { perror("you have error.\n"); exit(-1); } } //printf("\n"); fclose(fp); return 0; }
you don't want new line printed? don't print it!
remove this:
printf("\n"); it placed @ end of code.
edit
i gauss don't want remove completely, want print anytime except last time.
if (ftell(fp) != tot_file_size - 1) { printf("\n"); } edit v1.1
i ran code under linux mint 13 after improvment. output:
rw-r--r--a.txt/ rw-r--r--b.txt/ this format want, isn't it?
Comments
Post a Comment