我試圖模擬一個DNS服務器的行爲,我有一個DB名爲hosts.txt
含machine_names/IP_addresses
,例如:行爲異常處理用C
equipo_00/169.0.1.169
sala_oeste_01/200.1.2.200
sala_oeste_02/200.2.3.200
sala_oeste_03/200.3.4.200
MEMFIS_04/201.4.5.201
ICARO_05/200.5.6.200
equipo_06/169.6.7.169
sala_este_07/201.7.8.201
sala_este_08/201.8.9.201
CEC_09/189.9.10.189
這裏是我的代碼:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int main(int argc, char** argv)
{
char* machine, *ip_add;
FILE* db;
char* flag;
char tmp[256];
char par[256];
printf("> ");
scanf("%s", par);
db = fopen("hosts.txt", "r");
while(db != NULL && fgets(tmp, sizeof(tmp), db) != NULL)
{
if(strstr(tmp, par))
{
flag = "1"; // flag setting to 1 means find the line
machine = strtok(tmp, "/");//here's supposed to get the value of the machine
ip_add = strtok(NULL, "/");//here's supposed to get the value of the ip adress
}
}//while
if(strcmp(flag, "1") == 0) //
{
printf("here\n");
}
else
{
flag = "0"; //flag setting to 0
printf("there\n");
}
printf("flag= %s pc=%s server=%s\n", flag, machine, ip_add);
return 0;
}
我的代碼從標準輸入中讀取機器的值,並指出分配給服務器上該機器的IP地址。問題是程序不正常的行爲,我不知道如何修改我的代碼預期的輸出,如
input: equipo_00
output: flag = 1 pc = equipo_00 server=169.0.1.169
很抱歉,如果這個問題是相當愚蠢的,我是新來的這門語言..謝謝大家的幫助和原諒我的英語
什麼是錯的它現在/你的代碼看到了什麼問題? – 2010-12-03 07:28:22