几次运行后,fgets和strtok崩溃并出现Segmentation错误

时间:2013-03-19 21:10:33

标签: csv fgets strtok

我有一个csv文件,我读取以获取我需要进行查找的IP地址。我决定使用fgets和strtok来获取csv文件的每一行中的IP地址。 IP地址是文件中的第四个标记(我将计数器作为第一个标记开始计数)它读取行,对记录进行标记,但它似乎没有离开while循环来执行其余的查找过程使用我找到的IP地址令牌。此外,它似乎对文件进行了额外的读取,导致运行时出现Segmentation错误

这是代码..希望你可以协助,因为我是c的新手,我正在尝试使用c代替java,因为代码在c中运行得更快

#include "GeoIP.h"
#include "GeoIPCity.h"


static const char * _mk_NA( const char * p ){
 return p ? p : "N/A";
}

int 
main(int argc, char *argv[])
{
  FILE           *f;
  FILE           *out_f;
  GeoIP          *gi;
  GeoIPRecord    *gir;
  int             generate = 0;
  char            iphost[50];
  char            *nextWordPtr = NULL;
  int             wordCount =0;
  char            *rechost;
  char            recbuffer[1000];
  char delims[]=",";
  const char     *time_zone = NULL;
  char          **ret;
  if (argc == 2)
    if (!strcmp(argv[1], "gen"))
      generate = 1;

  gi = GeoIP_open("../data/GeoIPCity.dat", GEOIP_MEMORY_CACHE);

  if (gi == NULL) {
    fprintf(stderr, "Error opening database\n");
    exit(1);
  }

  f = fopen("city_test.txt", "r");

  if (f == NULL) {
    fprintf(stderr, "Error opening city_test.txt\n");
    exit(1);
  }

  out_f = fopen("out_city_lookup_test.txt", "w");

  if (out_f == NULL) {
    fprintf(stderr, "Error opening out_city_lookup_test.txt\n");
    exit(1);
  }

//** Read the file line by line and get the ip address to use to lookup GeoIP **//
//*     while (!feof(f)) {
   while (fgets(recbuffer,1001,f) != NULL {
         nextWordPtr = strtok (recbuffer,delims); 
         while (nextWordPtr != NULL & wordCount < 5) {
           printf("word%d %s\n",wordCount,nextWordPtr);
           if (wordCount == 4 ) {
               printf("nextWordPtr %s\n",nextWordPtr);
               strcpy(iphost, nextWordPtr);
               printf("iphost %s\n",iphost);
           }    
           wordCount++;
           nextWordPtr = strtok(NULL,delims);
         }
    gir = GeoIP_record_by_name(gi, (const char *) iphost);

    if (gir != NULL) {
      ret = GeoIP_range_by_ip(gi, (const char *) iphost);
      time_zone = GeoIP_time_zone_by_country_and_region(gir->country_code, gir->region);
      printf("%s\t%s\t%s\t%s\t%s\t%s\t%f\t%f\t%d\t%d\t%s\t%s\t%s\n", iphost,
         _mk_NA(gir->country_code),
         _mk_NA(gir->region),
         _mk_NA(GeoIP_region_name_by_code(gir->country_code, gir->region)),
         _mk_NA(gir->city),
         _mk_NA(gir->postal_code),
         gir->latitude,
         gir->longitude,
         gir->metro_code,
         gir->area_code,
         _mk_NA(time_zone),
         ret[0],
         ret[1]);
      fprintf(out_f,"%s\t%s\t%s\t%s\t%s\t%s\t%f\t%f\t%d\t%d\t%s\t%s\t%s\n", iphost,
         _mk_NA(gir->country_code),
         _mk_NA(gir->region),
         _mk_NA(GeoIP_region_name_by_code(gir->country_code, gir->region)),
         _mk_NA(gir->city),
         _mk_NA(gir->postal_code),
         gir->latitude,
         gir->longitude,
         gir->metro_code,
         gir->area_code,
         _mk_NA(time_zone),
         ret[0],
         ret[1]);
      GeoIP_range_by_ip_delete(ret);
      GeoIPRecord_delete(gir);
    }
  }
  GeoIP_delete(gi);

  fclose(out_f);

  return 0;

}

0 个答案:

没有答案