ar_hdr 형식의 새 파일 멤버를 추가하고 아카이브의 마지막 요소 바로 뒤에 넣으려고합니다. 내 코드가 컴파일되지만 ar -t 명령으로 파일 이름을보고 싶을 때 다음과 같은 오류 메시지가 나타납니다. ar : hello.a : 부적절한 파일 형식 또는 형식. 누군가 내 코드를 살펴보고 해결 방법에 대한 힌트를 줄 수 있습니까? 감사."부적절한 파일 형식 또는 형식"
#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>
#define BLOCKSIZE 1
int main (int argc, char **argv)
{
char *archive = argv[1];
char *read_file = argv[2];
int in_fd;
int out_fd;
char title[] = ARMAG; //constant define in ar.h
char buf[BLOCKSIZE];
int num_read;
int num_written;
struct stat stat_file;
struct ar_hdr my_ar;
//open read_file (i.e., text file)
if (stat(read_file, &stat_file) == -1){
perror("Error in Stat");
exit(-1);
}
//assign file info to struct dhr (my_ar)
sprintf(my_ar.ar_name, "%s", read_file);
sprintf(my_ar.ar_date, "%ld", stat_file.st_mtimespec.tv_sec);
sprintf(my_ar.ar_uid, "%i", stat_file.st_uid);
sprintf(my_ar.ar_gid, "%i", stat_file.st_gid);
sprintf(my_ar.ar_mode, "%o", stat_file.st_mode) ;
sprintf(my_ar.ar_size, "%lld", stat_file.st_size) ;
//0666 - open archive
out_fd = open(archive, O_CREAT | O_WRONLY | O_APPEND, 0666);
if (out_fd == -1) {
perror("Canot open/create output file");
exit(-1);
}
//write my_ar to archive
num_written = write(out_fd, title, sizeof(title));
num_written = write(out_fd, &my_ar, sizeof(my_ar));
printf("\n");
return 0;
}
'ar_fmag' 구조체 멤버를 설정하지 않을 수도 있습니다 (http://docs.oracle.com/cd/E19082-01/819-2242/ar 참조).h-3head/index.html) - 'ar'에 대한 경험이 없으므로 추측입니다. 또한'od'를 사용하여 출력 파일을 분석하여 예상 한 바이트 수를 확인하는 것이 유용 할 수 있습니다. – imp25