2010-12-03 21 views
1
文本文件

我試圖模擬一個DNS服務器的行爲,我有一個DB名爲hosts.txtmachine_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 

很抱歉,如果這個問題是相當愚蠢的,我是新來的這門語言..謝謝大家的幫助和原諒我的英語

+1

什麼是錯的它現在/你的代碼看到了什麼問題? – 2010-12-03 07:28:22

回答

4

這是你做什麼:

從文件中讀取一個字符串轉換爲tmp

檢查用戶輸入的字符串par是否存在於tmp中。

如果是你繼續和記號化的/tmp,使指針machine點到第一支和ip_add點到下一段。

請注意,machineip_add只是指針。他們指出tmp的各種指數。因此,稍後當您繼續循環時,您將新字符串再次讀入tmp,覆蓋它。這會導致問題,您的指針現在指向更改的字符串。

爲了避免這種情況只是一個成功的比賽之後添加一休:

if (strstr(tmp, par)) { 
    flag = "1"; 
    machine = strtok(tmp, "/"); 
    ip_add = strtok(NULL, "/"); 
    break;  
} 

而且最終printf

printf("flag= %s pc=%s server=%s\n", flag, machine, ip_add); 

應該是你if身體的一部分,所以你只要把它們打印你找到了這場比賽。 即使找不到匹配項,您仍然正在打印。在這種情況下,您將打印垃圾,因爲您的指針serverip_add未初始化

+1

+1,用於像許多人那樣推薦`break'而不是`strdup`。 – 2010-12-03 10:49:31

0

它看起來像你的問題可能與flag變量。使用字符串作爲標誌值看起來很不尋常。試試這個:

int flag = 0; // always initialise your variables 

if (strstr(tmp, par) == 0) { 
    flag = 1; // just an integer value 
} 

if (flag) { // easier to test the value too! 
    // ... 
} 

printf("flag=%d\n", flag); // ints use %d format value 

在上面的代碼中,flag變量在其不會可能會意外(和undefined)結果函數的開始未初始化。

0

下面是固定的代碼。有幾個小錯誤,主要是缺少inits。想想如果機器沒有找到會發生什麼。

最主要的是你在循環的每次迭代中使用相同的緩衝區,因此清理了以前找到的服務器。我通過退出循環來解決這個問題。另一種方法可能是將找到的結果複製到另一個緩衝區中。

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
#include <unistd.h> 

int main(int argc, char** argv) 
{ 
    char* machine = ""; 
    char* 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) 
    { 
     char * found = strstr(tmp, par); 
     if(found) 
     { 
      flag = "1"; // flag setting to 1 means find the line 
      machine = strtok(found, "/");//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 
      break; 
     } 
    }//while 

    if(strcmp(flag, "1") == 0) 
    { 
     flag = "0"; //flag setting to 0 
    } 

    printf("flag= %s pc=%s server=%s\n", flag, machine, ip_add); 
    return 0; 
} 
0
if(strcmp(flag, "1") == 0) 

你不能檢查,因爲如果標誌沒有initializied這是不確定的行爲

的strcmp()搜尋SEARCH_TERM_EXAMPLES在兩個參數「\ 0」字符