缺少最后一个IP地址

时间:2015-11-03 19:00:49

标签: c arrays multidimensional-array

我有一个文件,我正在尝试将ip地址存储到一个数组中。我的计划是将数据存储到逗号或将地址存储到虚拟变量之后存储数据。由于某种原因,我的代码可以运行到第二行。任何帮助都会很棒。

Blacklist.csv:

46.30.45.65, good.recycle2learn.com
22.51.66.23, hothouse.com
83.98.11.48, monkeyhouse.com

代码:

void Blacklist()
{
  FILE *blist;
  char quotations = 0;

  blist = fopen("blacklist.csv", "r");
  if (blist == NULL)
  {
    //Quit if something goes wrong
    exit(EXIT_FAILURE);
  }

  for (int i = 0; i <= 3; i++)
  {
    fscanf(blist, "%d.%d.%d.%d,", &blacklist[i][0], &blacklist[i][1],
        &blacklist[i][2], &blacklist[i][3]);
    while (quotations != '\n')
    {
      fscanf(blist, "%c,", &quotations);
    }
  }

  for (int i = 0; i <= 3; i++)
  {
    printf("%d.%d.%d.%d\n", blacklist[i][0], blacklist[i][1], blacklist[i][2],
        blacklist[i][3]);
  }
}

1 个答案:

答案 0 :(得分:4)

在阅读完每一行后,您没有重置quotations变量(根据其使用的内容,该变量名称不是很好)。到达第一个换行符后,您的while循环将永远不会再次输入。如果你在调试器中运行代码,你会看到发生这种情况。

在读取IP地址后重置变量:

for (int i = 0; i < 3; i++) // <-- use < instead of <=
{
    fscanf(blist, "%d.%d.%d.%d,", &blacklist[i][0], &blacklist[i][1], &blacklist[i][2], &blacklist[i][3]);
    quotations = 0; // <-- add this
    while (quotations != '\n')
    {
        fscanf(blist, "%c", &quotations); // <-- no comma after %c
    }
}

或者使用do..while循环代替:

for (int i = 0; i < 3; i++)
{
    fscanf(blist, "%d.%d.%d.%d,", &blacklist[i][0], &blacklist[i][1], &blacklist[i][2], &blacklist[i][3]);
    do
    {
        fscanf(blist, "%c", &quotations);
    }
    while (quotations != '\n');
}

更新:话虽如此,您的代码不会处理csv文件在每个IP条目之间包含空行的可能性(如您的问题中所示)或最后一个丢失的换行符条目。为了解决这些问题,请尝试使用fgets()getline()逐行读取文件到本地缓冲区,使用sscanf()根据需要解析缓冲区。

int blacklist[3][4];
int numblacklist;

void Blacklist()
{
  FILE *blist;
  char line[64];

  memset(blacklist, 0, sizeof(blacklist));
  numblacklist = 0;

  blist = fopen("blacklist.csv", "r");
  if (blist == NULL)
  {
    //Quit if something goes wrong
    exit(EXIT_FAILURE);
  }

  while (fgets(line, 64, blist))
  {
    if (sscanf(line, "%d.%d.%d.%d,", &blacklist[numblacklist][0], &blacklist[numblacklist][1], &blacklist[numblacklist][2], &blacklist[numblacklist][3]) == 4)
    {
      numblacklist++;
      if (numblacklist == 3)
        break;
    }
  }

  fclose(blist);

  for (int i = 0; i < numblacklist; i++)
  {
    printf("%d.%d.%d.%d\n", blacklist[i][0], blacklist[i][1], blacklist[i][2], blacklist[i][3]);
  }
}

或者:

int blacklist[3][4];
int numblacklist;

void Blacklist()
{
  FILE *blist;
  size_t linelen;
  char *line;

  memset(blacklist, 0, sizeof(blacklist));
  numblacklist = 0;

  blist = fopen("blacklist.csv", "r");
  if (blist == NULL)
  {
    //Quit if something goes wrong
    exit(EXIT_FAILURE);
  }

  line = NULL;
  linelen = 0;

  while (getline(&line, &linelen, blist) != -1)
  {
    if (sscanf(line, "%d.%d.%d.%d,", &blacklist[numblacklist][0], &blacklist[numblacklist][1], &blacklist[numblacklist][2], &blacklist[numblacklist][3]) == 4)
    {
      numblacklist++;
      if (numblacklist == 3)
        break;
    }
  }

  free(line);
  fclose(blist);

  for (int i = 0; i < numblacklist; i++)
  {
    printf("%d.%d.%d.%d\n", blacklist[i][0], blacklist[i][1], blacklist[i][2], blacklist[i][3]);
  }
}