指针问题没有解决

时间:2016-11-04 13:09:43

标签: c pointers

在下面的代码中,文件test.txt包含以下数据:

192.168.1.1-90    
192.168.2.2-80    

此输出不符合预期。我希望输出为

192.168.1.1    
90     
192.168.2.2   
80

当前输出

192.168.2.2    
80     
192.168.2.2     
80

我知道str的指针也指向第二次迭代中的相同地址。

我只是不能解决问题。 任何帮助将不胜感激。

#include <string.h> 
#include <stdio.h>
#include <stdlib.h>
int main() {
  FILE * fp;
  char * result[10][4];
  int i = 0;
  const char s[2] = "-";
  char temp[50];
  char * value, str[128], * string, t[20], x[29] = "192.168.2.2";
  fp = fopen("test.txt", "r");
  if (fp == NULL)
    printf("File doesn't exist\n");
  else {
    while (!feof(fp)) {

      if (fgets(str, sizeof(str), fp)) {

        /* get the first value */
        value = strtok(str, s);

        result[i][0] = value;
        printf("IP : %s\n", result[i][0]); //to be removed after testing


        /* get second value */
        value = strtok(NULL, s);

        result[i][1] = value;
        printf("PORT : %s\n", result[i][1]); //to be removed after testing
        i++;
      }
    }
    for (int k = 0; k < 2; k++) {
      for (int j = 0; j < 2; j++) {
        printf("\n%s\n", result[k][j]);
      }
    }

  }
  return (0);
}

4 个答案:

答案 0 :(得分:1)

我建议这样:

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

enum { IP = 0, PORT = 1};

int main(void){
    FILE *fp;
    char result[2][2][16];//2 lines, 2 kinds, 16:XXX.XXX.XXX.XXX+NUL
    const char *s = "-";//delimiter
    char *value, line[128];
    int i=0;

    fp = fopen("test.txt", "r");
    if (fp == NULL) {
        printf("File doesn't exist\n");
        exit(EXIT_FAILURE);
    }
    while(i < 2 && fgets(line, sizeof(line), fp)){
        value = strtok(line, s);
        strcpy(result[i][IP], value);
        printf("IP : %s\n",result[i][IP]);

        value = strtok(NULL, s);
        strcpy(result[i][PORT], value);
        printf("PORT : %s\n",result[i][PORT]);
        i++;
    }
    puts("");
    for (int k=0;k<2;k++){
        for (int j=0;j<2;j++){
            printf("%s\n",result[k][j]);
        }
    }
    fclose(fp);

    return 0;
}

答案 1 :(得分:1)

你做错了是你正在分配&#34;价值&#34;指向&#34;结果&#34;元素的指针阵列。在您的实现中,&#34;结果&#34;的所有元素;只是反映了&#34;价值&#34;指针。因此,当您更改&#34; value&#34;的值时,您还会更改所有&#34;结果&#34;元素。

因此,在为特定的&#34;结果&#34;分配内存之后,你应该使用strcpy函数。元件。

value = strtok(str, s);
result[i][0]=malloc(strlen(value) + 1);
strcpy(result[i][0], value);

答案 2 :(得分:0)

如果要保留复制字符串,则必须使用函数strcpy()

而不是result[i][x] = value,您应该执行以下操作

strcpy(result[i][x], value);

编辑:在strcpy之前,您必须使用mallocresult[i][x]字符串分配内存。 例如:

result[i][0] = malloc(10 * sizeof(char));

答案 3 :(得分:0)

我建议使用malloc为每个ipport分配空间,并在最后使用free释放它们。此外,如果您将来要使用更大的文本文件,那么结构可能在这里很方便。

代码:

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

#define COLS 2
#define MAXCHAR 10
#define BUFFSIZE 128

void exit_if_null(void *ptr, const char *msg);

int
main(void) {
    FILE *filename;
    char *result[COLS][MAXCHAR+1];
    char buffer[BUFFSIZE];
    char *ip, *port;
    int row, i = 0;

    filename = fopen("ips.txt", "r");

    if (filename == NULL) {
        fprintf(stderr, "%s\n", "Error reading file!\n");
        exit(EXIT_FAILURE);
    } else {
        while (fgets(buffer, BUFFSIZE, filename) != NULL && i < 2) {
            ip = strtok(buffer, "-");
            port = strtok(NULL, "\n");

            result[i][0] = malloc(strlen(ip)+1);
            exit_if_null(result[i][0], "Initial Allocation");

            result[i][1] = malloc(strlen(port)+1);
            exit_if_null(result[i][1], "Initial Allocation");

            strcpy(result[i][0], ip);
            strcpy(result[i][1], port);

            i++;
        }
    }

    for (row = 0; row < i; row++) {
        printf("%s\n", result[row][0]);
        printf("%s\n", result[row][1]);

        free(result[row][0]);
        free(result[row][1]);

        result[row][0] = NULL;
        result[row][1] = NULL;
    }

    return 0;
}

void
exit_if_null(void *ptr, const char *msg) {
    if (!ptr) {
        printf("Unexpected null pointer: %s\n", msg);
        exit(EXIT_FAILURE);
    }
}