- 论坛徽章:
- 0
|
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include <stdio.h>
- void ShowMenu();
- int AddRecord();
- int RmvRecord();
- int LstRecord();
- #define DAT_FILE ".DOMAIN_FILE"
- typedef struct StructDomain
- {
- // int did;
- char domain[64];
- char ip[64];
- }StructDomain;
- int main(int argc, char ** argv)
- {
- char key[128];
- ShowMenu();
- while (scanf("%s", key))
- {
- fflush(NULL);
- //getchar();
- switch(key[0])
- {
- case 'a':
- case 'A':
- if (0 != AddRecord())
- {
- printf("Error occur.\n");
- return 1;
- }
- break;
- case 'r':
- case 'R':
- RmvRecord();
- break;
- case 'l':
- case 'L':
- LstRecord();
- break;
- case 'x':
- case 'X':
- return 0;
- default:
- ShowMenu();
- }
- fflush(NULL);
- }
- return 0;
- }
- void ShowMenu()
- {
- printf("\n");
- printf("-----------------------------------------\n");
- printf("--------- Domain Manage Tool ------------\n");
- printf(" [a] Add Domain \n");
- printf(" [r] Rmv Domain \n");
- printf(" [l] Lst Domain \n");
- printf(" [x] Exit \n");
- printf("-----------------------------------------\n");
- printf("choose: [x] ");
- }
- int AddRecord()
- {
- StructDomain domain;
- int fd;
- printf("\nPlease input Domain: ");
- fflush(NULL);
- scanf("%s", domain.domain);
- printf("Please input IP: ");
- fflush(NULL);
- scanf("%s", domain.ip);
- fd = open(DAT_FILE, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
- if (-1 == fd)
- {
- return 1;
- }
- lseek(fd, SEEK_END, 0);
- if (sizeof(domain) != write(fd, &domain, sizeof(domain)))
- {
- return 1;
- }
- close(fd);
- return 0;
- }
- int RmvRecord()
- {
- printf("RmvRecord\n");
- return 0;
- }
- int LstRecord()
- {
- int fd;
- StructDomain domain;
- fd = open(DAT_FILE, O_RDONLY);
- if (-1 == fd)
- {
- return 1;
- }
- while (sizeof(domain) == read(fd, &domain, sizeof(domain)))
- {
- printf("Domain:%s IP:%s\n", domain.domain, domain.ip);
- }
- close(fd);
- return 0;
- }
复制代码
执行结果如下:
[cheyo@Fedora]$ ./domain
-----------------------------------------
--------- Domain Manage Tool ------------
[a] Add Domain
[r] Rmv Domain
[l] Lst Domain
[x] Exit
-----------------------------------------
choose: [x] a
Please input Domain: sina.com
Please input IP: 1.3.4.5
l
Domain: IP:
a
Please input Domain: cu.net
Please input IP: 3.4.5.5
l
Domain: IP:
x
[cheyo@Fedora]$
原帖由 MMMIX 于 2007-5-2 11:23 发表
如果方便的话给出完整的代码以及测试执行的结果。 |
|