开发者

File reading trouble?

typedef long int INT;
typedef unsigned char byte;

#define MAX_CITIES (INT)2.7e6
#define MAX_LEN (int)1e6

typedef struct
{
    unsigned long start;
    unsigned long end;
    char *region;
    char *city;
    char *type;
} ipdb;

ipdb db;
char s[MAX_LEN + 1];


typedef struct
{
    unsigned long start;
    unsigned long end;
    char *region;
    char *city;
    char *type;
} ipdb;

ipdb db;    
void search( unsigned long ip )
{
    FILE *F;
    char * pch;
    long i = 0;
    F = fopen("cidr_ru_slave_index.db", "rt");
    while (fgets(s, MAX_LEN, F))
    {
        trim_end(s);
        pch = strtok (s, "\t");
        i = 0;
        while (pch != NULL)
        {
            switch(i)
            {
                case 0:
                    db.start = atoi(pch);
                break;

                case 1:
                    db.end = atoi(pch);
                break;

                case 4:
                    db.city = pch;
          开发者_StackOverflow社区      break;

                case 5:
                    db.region = pch;
                break;

                case 8:
                    db.type = pch;
                break;
            }
            pch = strtok (NULL, "\t");
            i++;
        }

        if(ip >= db.start && ip <= db.end)
        {
            printf("Match\n");
        }
    }
}

The cidr_ru_slave_index.db is look like this:

37486592    37494783    2.60.0.0 - 2.60.31.255  RU  Омск    Омская область  Сибирский   ASSIGNED PA
37494784    37502975    2.60.32.0 - 2.60.63.255 RU  Омск    Омская область  Сибирский   ASSIGNED PA
37502976    37511167    2.60.64.0 - 2.60.95.255 RU  Омск    Омская область  Сибирский   ASSIGNED PA
37511168    37519359    2.60.96.0 - 2.60.127.255    RU  Омск    Омская область  Сибирский   ASSIGNED PA
37519360    37527551    2.60.128.0 - 2.60.159.255   RU  Омск    Омская область  Сибирский   ASSIGNED PA
...
3653754624  3653754879  217.199.223.0 - 217.199.223.255 RU  Москва  Москва  Центральный ASSIGNED PA
3653758976  3653760255  217.199.240.0 - 217.199.244.255 RU  Екатеринбург    Свердловская область    Уральский   ASSIGNED PA
3653759488  3653759503  217.199.242.0 - 217.199.242.15  RU  Екатеринбург    Свердловская область    Уральский   ASSIGNED PA
3653759520  3653759551  217.199.242.32 - 217.199.242.63 RU  Екатеринбург    Свердловская область    Уральский   ASSIGNED PA
3653759520  3653759535  217.199.242.32 - 217.199.242.47 RU  Екатеринбург    Свердловская область    Уральский   ASSIGNED PA
3653762560  3653762815  217.199.254.0 - 217.199.254.255 RU  Екатеринбург    Свердловская область    Уральский   ASSIGNED PA

The file size is 14718KB and it reads ok (adding printf("%u\n", db.start) after

}
                pch = strtok (NULL, "\t");
                i++;
            }

outputs all file). But there are trouble with scanning for ip address (if(ip >= db.start && ip <= db.end)), the first half of file is ok, but after first half there are not any result, why?

PS OS - gnu/linux PSS sorry for my english :D


Values of 2^31 and higher are outside the range that atoi can handle and it'll return INT_MAX for them. You could use strtoul instead.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜