sysprog(2007. 9. 20)

Posted 2007. 9. 20. 14:42 by freax
#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

파일이름을 사용자가 입력하게 해서 인자로 받아서 사용.(전주 과제 참조.)
*/




// 퍼가는건 좋은데 인사는 하고 갑시다

'homeworks。' 카테고리의 다른 글

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. 27, 완)  (0) 2007.09.27