#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;
int main(int argc, char** argv)
{
FileEntry index[1000];
int nentry;
int fd;
int i;
char service[10];
char word[100];
fd = open("file.idx", O_RDONLY);
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)
{
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)
{
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("file.idx", O_WRONLY | O_TRUNC);
write(fd, &nentry, sizeof(int));
write(fd, &index, sizeof(FileEntry)*nentry);
close(fd);
}
/*
1. file.idx를 만든다
2. ./test
3. service : insert
4. exit
5. ./test
6. service : search
파일이름을 사용자가 입력하게 해서 인자로 받아서 사용.(전주 과제 참조.)
*/
// 퍼가는건 좋은데 인사는 하고 갑시다