c 질문요 sprintf
Sweet
위 프로그램은 프로그램리스트를 링크드리스트로 연결하고 출력하는 프로그램입니다.아래 sprintf 에서 쌍따옴표를 쓰기 위해서 \%s\ 이렇게 사용하였습니다그런데 결과값이 이상하게나오네요
리눅스에서 ls 첫을때 나오는 첫 파일만 검색이 됩니다저 \ \를 지우고 인자값을 넘길때
./프로그래명 ./ \*\ 이렇게 넘기면 잘됩니다원인을 모르겠네요\%s\ 해서 argv[2] 를 출력하면 *을 넘겨도 *이 아니라 첫번째 찾아지는 파일명이 나오더라구요그래서 find ./ -name 첫번째파일명이렇게 되어서 하나만 찾아집니다.아시는분 있으신가요?
#include stdio.h
#include stdlib.h
#include string.h
#include dirent.h
#include sys/types.h
#include sys/stat.h
#include unistd.h
int number = 0;typedef struct linked_list{
int number;
char path[2048];
linked_list *next;
}list;void delnode(list *phead){
list *tempnode; while( phead-next != null ){
tempnode = phead;
phead = phead-next;
free(tempnode);
}
free(phead);
printf(메모리 정리 완료 \n);
}void addnode(char* command,list *phead){
number++;
list *tempnode = phead;
list *newnode = (list*)malloc(sizeof(list));
newnode-number = number; strcpy(newnode-path,command);
newnode-next = null; while(tempnode-next != null){
tempnode = tempnode-next;
} tempnode-next = newnode;
newnode-next = null;
}
void ptrnode(list *phead){ list *tempnode = phead;
tempnode = tempnode-next;
while(tempnode-next != null){
printf(%d. %s,tempnode-number,tempnode-path);
tempnode = tempnode-next;
}
printf(%d. %s,tempnode-number,tempnode-path);
}int main(int argc, char **argv){ file *fp;
char command[1024];
list *phead;
phead =(list *)malloc(sizeof(list));
phead-next = null;
phead-number = 0; //argv[0] 탐색경로
//argv[1] 찾을 찾을 파일명 if(argc 3){
printf(usage: %s dir , %s filename\n,argv[1],argv[2]);
exit(1);
}
printf(argv[2] = %s\n,argv[2]);
sprintf(command,find %s -name \%s\ ,argv[1],argv[2]);
printf(command = %s\n,command);
fp = popen(command, r);
while (fgets(command, sizeof(command), fp) != null) {
//printf(while()\n);
addnode(command,phead);
} pclose(fp);
printf(####### file list ########\n);
ptrnode(phead);
printf(###########################\n);
delnode(phead);
}
질문 제목 : 질문 내용 :