C를 사용하여 각 경로에 파일 이름을 연결하여 stat()를 호출 할 수 있지만 strcat를 내부에서 사용하려고 할 때 루프는 이전 파일 이름을 다음 파일과 연결합니다.경로로 파일을 연결하여 C로 전체 경로 가져 오기
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <string.h>
int main(int argc, char *argv[]) {
struct stat buff;
int status;
if (argc > 1) {
status = stat(argv[1], &buff);
if (status != -1) {
if (S_ISDIR(buff.st_mode)) {
DIR *dp = opendir(argv[1]);
struct dirent *ep;
char* path = argv[1];
printf("Path = %s\n", path);
if (dp != NULL) {
while (ep = readdir(dp)) {
char* fullpath = strcat(path, ep->d_name);
printf("Full Path = %s\n", fullpath);
}
(void) closedir(dp);
} else {
perror("Couldn't open the directory");
}
}
} else {
perror(argv[1]);
exit(1);
}
} else {
perror(argv[0]]);
exit(1);
}
return 0;
}
가이 코드에 구문 오류가 있습니다; "perror (argv [0]]);" 하나가 너무] 많습니다. 실제 코드를 복사하여 붙여 넣으십시오. –