숙제를 위해 나는 C 언어로 어쿠스틱 파일에 저장된 파일의 사용 권한과 파일 이름을 인쇄하는 코드를 작성했습니다. 내 코드가 컴파일되지만 어떤 이유로 두 번째 파일의 이름이 새 행에 반환됩니다.이 인쇄 출력 문제를 어떻게 해결할 수 있습니까?
rw-r--r-- 501/20 1-s.txt
rw-r--r-- 501/20 2-s.txt
(no empty blank line here)
사람이 내 코드를 살펴보고 좀 줄 수 :
rw-r--r-- 501/20 1-s.txt
rw-r--r-- 501/20
2-s.txt
(empty blank line here)
이 내가 출력이보고 싶은 방법입니다 : 여기
내 출력 단자에서 같은 모습입니다 내 코드를 수정하는 방법에 대한 도움말? 나는 그것을 읽는 방법과 파일 이름 (my_ar.ar_name
사용)을 선택하는 것과 관련이 있다고 생각하지만, 그것을 고치는 방법을 알 수는 없다. 시간 내 주셔서 감사합니다.
#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 the archive file (e.g., hw.a)
fp = fopen(argv[1], "r");
if (fp == NULL)
{
perror("Error opening the file\n");
exit(-1);
}
//size of the archive file
fseek(fp, 0, SEEK_END);
tot_file_size =ftell(fp);
rewind(fp);
//read data into 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 an error.\n");
exit(-1);
}
}
//printf("\n");
fclose(fp);
return 0;
}
내가 올바른 제거 최근'의 printf ("\ n")를 생각하면, '나는 내 옆에있는 터미널 명령은 "2-s.txt"와 같은 줄에 표시 것을 제거하면 FCLOSE 문 –
전 . – user2203774