二维char数组问题

时间:2013-10-09 19:35:04

标签: c

我发誓我真的是一个不错的程序员,但是我用Java编程多年后在C编程中的冒险让我很生气。

我正在尝试使用一组IP地址/端口对填充二维字符数组。我正在从文件中读取它们。正确地将它们从文件中拉出,并且应该正确地放入阵列中。问题在于,由于某种原因,当第二组被放入阵列时,它会覆盖第一组,我不能为我的生活找出原因。

文件的第一行是文件中的IP地址/端口对的数量(我称之为元组)。以下行是由空格分隔的IP地址和端口。

以下是代码:

 //read the top line with the number of items
  fgets(line, sizeof line, fp);
  numLines = atoi(line);
  printf("%s %d\n","numLines:",numLines);
  char* tuples[numLines][2];
  char* rawLines[numLines];
  //read each line and put it into array
  for(currentLine=0; currentLine<numLines; currentLine++){
    if(fgets(line, sizeof line, fp) == NULL){
      perror("fgets");
      return -1;
    }
    printf("%s %d \n","curentLine: ",currentLine);
    char* port;
    tuples[currentLine][0] = strtok(line, " ");
    printf("%s %s \n", "IP Address: ", tuples[currentLine][0]);
    //rawLines[currentLine] = line;
    port = strtok(NULL, " ");
    size_t ln = strlen(port) - 1;
    if (port[ln] == '\n')
      port[ln] = '\0';
    tuples[currentLine][1]=port;
    printf("%s %s\n","port: ", tuples[currentLine][1]);
  }
  //list created and stored in tuples
  //now that list is created choose a random server from the file and strip the value chosen from the list

  //choose random server
  srand (time(NULL));
  //randomServer = rand()%numLines;
  randomServer = 0;
  printf("%s %d\n", "randomServer: ", randomServer);



  //connect to random server pulled
  memset(&hints, 0, sizeof hints); // make sure the struct is empty
  hints.ai_family = AF_UNSPEC;     // don't care IPv4 or IPv6
  hints.ai_socktype = SOCK_STREAM; // TCP stream sockets
  hints.ai_flags = AI_PASSIVE;     // fill in my IP for me
  //setup client socket
  printf("%s %s \n", "Setting up connection to: ", tuples[randomeServer][0]);
  printf("%s %s \n", "Setting up connection on port: ", tuples[randomServer][1]);

以下是我得到的输出:

numLines: 2
curentLine:  0
IP Address:  127.0.0.1
port:  3761
curentLine:  1
IP Address:  192.168.0.1
port:  3762
randomServer:  0
Setting up connection to:  192.168.0.1
Setting up connection on port:  1

我期望获得的是: 设置连接:127.0.0.1 在端口上设置连接:3761

如果我在文件中只有一行,那么我会得到预期的值。

提前谢谢。

4 个答案:

答案 0 :(得分:2)

不是直接将strtok返回给您的二维数组,而是使用strcpy复制内容:

char *ipAddress = strtok(line, " ");
char *tuple0 = malloc(sizeof(char) * (strlen(ipAddress) + 1));
strcpy(tuple0, ipAddress);
tuples[currentLine][0] = tuple0;

答案 1 :(得分:0)

问题是您没有正确复制数据。您有一个名为line的变量,它似乎是char的数组,您使用fgets()将输入的每一行读入此变量。然后对输入进行标记化,对于每个标记,您将指针存储到line数组中,并存储到tuples数组中的某个位置,但此数据会被覆盖为你在下一行读到的。你真正需要做的是分配一个新的存储空间,将数据复制到其中,并将指向新存储的指针存储到tuples数组中。

答案 2 :(得分:0)

好像你没有为“已保存”的字符串分配内存。您的声明:

char* tuples[numLines][2];
char* rawLines[numLines];

声明指向char的指针数组。不是“字符串”。所以你错过了类似的东西:

tuples[index_value][0] = malloc(number_of_characters); 

然后你应该使用strcpy或strncpy写下你在该内存中读取的行。

sizeof(char) is always one

答案 3 :(得分:0)

值得记住的是,C中没有2D数组.2D数组是一个抽象概念,可以在C中实现(至少)两种不同的具体结构。有阵列数组,还有指针数组。

当我们讨论指针数组时,我们通常(并非总是)排列它们,使得各个指针指向不同的内存块,以免我们的抽象2D数组最终包含同一行的重复项。为了实现这一点,我们通常在堆上显式分配每一行,然后用值填充它。请注意,strtok不会分配任何内容。它接受一个字符数组并返回指向其中的指针(并销毁那里的原始字符串)。

相关问题