#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/dir.h>
#include <dirent.h>
#include <stdio.h>
//정보를 저장할 구조체 선언
typedef struct{
char name[100];
char key[100];
char pathname[1000];
}FileEntry;
//사용자의 입력을 받기위해 main함수 변경
main(argc, argv)
int argc;
char *argv[];
{
FileEntry index[1000];
int nentry;
int fd;
int i;
char service[10];
char word[100];
//사용자의 입력을 받은 파일이름으로 읽기전용으로 파일을 만든다
fd = open(argv[1], O_RDONLY | O_CREAT);
// 파일을 읽어들인다
read(fd, &nentry, sizeof(int));
read(fd, &index, sizeof(FileEntry)*nentry);
close(fd);
while(1)
{
// 사용자의 입력을 받아 서비스를 실행(입력, 검색, 종료)
printf("Service : [i : insert, s : search, e : exit]");
scanf("%s", service);
// 입력을 할경우
if(strcmp(service, "i") == 0)
{
//사용자로부터 이름, 키워드, path의 정보를 입력 받는다
printf("Name : ");
scanf("%s", index[nentry].name);
printf("Keyword : ");
scanf("%s", index[nentry].key);
printf("Pathname : ");
scanf("%s", index[nentry].pathname);
++nentry;
}
// 검색할경우
else if(strcmp(service, "s") == 0)
{
// name이나 keyword를 입력을 받는다
printf("Name or Keyword : ");
scanf("%s", word);
for(i=0; i<nentry ; ++i)
{
if(!strcmp(index[i].name, word) ||!strcmp(index[i].key, word))
{
printf("Found : %s \n",index[i].pathname);
}
}
}
else
break;
}
//최초의 입력받은 파일이름으로 쓰기전용으로 연다. 파일내용이 있으면 무시한다.
fd = open(argv[1], O_WRONLY | O_TRUNC);
write(fd, &nentry, sizeof(int));
write(fd, &index, sizeof(FileEntry)*nentry);
close(fd);
}
sysprog(과제, 실행화면) (0) | 2007.10.09 |
---|---|
2007. 10. 4(실습자료) (0) | 2007.10.04 |
sysprog 홈페이지 (0) | 2007.10.04 |
sysprog(2007.9.27, 실습) (0) | 2007.09.27 |
sysprog(2007. 9. 20) (0) | 2007.09.20 |