使用C的getline时出现奇怪的行为()

时间:2016-08-20 13:45:47

标签: c arrays string input file-io

我在编写一个小的c程序时在stdout函数中遇到了一些奇怪的行为。我想做什么:

  1. stderr重定向到文件
  2. stdin重定向到文件
  3. stdin重定向到文件
  4. 逐行将char *path_array读入字符串数组(retrieving line of size 73. line1 llllllllllllskdjflaksdlfkalskdddddddffffffffffffffffffffffffffffff retrieving line of size 6. line2 retrieving line of size 6. line3 retrieving line of size 6. line4 retrieving line of size 6. line5 retrieving line of size 6. line6 retrieving line of size 6. line7 retrieving line of size 6. line8 retrieving line of size 1. retrieving line of size 13. sdkfjlskdfos retrieving line of size 9. sldjflsd retrieving line of size 9. sdlfkjsd retrieving line of size 11. 2222222222 retrieving line of size 11. 3333333333 retrieving line from array: retrieving line from array: retrieving line from array: retrieving line from array: retrieving line from array: retrieving line from array: retrieving line from array: retrieving line from array: retrieving line from array: retrieving line from array: retrieving line from array: retrieving line from array: retrieving line from array: retrieving line from array:
  5. 打印输出
  6. 现在,当我运行程序时,输出如下所示:

    retrieving line from array

    在此输出结束时,在每个for下面,应该有来自数组的相应字符串。如您所见,数组中填充了空字符串。

    使用Clion的调试模式,我发现了为什么会这样:

    在第一个line循环中,每个数组条目都填充了当前的path_array[0] = line1。而不是

    path_array[1] = line2

    path_array[3] = line3

    path_array[0] = line1 ...

    它去了

    path_array[0] = line2, path_array[1] = line2

    path_array[0] = line3, path_array[1] = line3, path_array[2] = line3

    #include <zconf.h> #include <dirent.h> #include <stdlib.h> #include <fcntl.h> #include <stdio.h> #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wmissing-noreturn" int main(void) { int out_log; int err_log; int conf_in; pid_t pid = fork(); DIR *dir; struct dirent *entry; //for getline() char *line = NULL; size_t len = 0; ssize_t read; int array_size = 0; // if (pid < 0) { // exit(1); // } // else if (pid > 0) { // exit(0); // } // // if (setsid() <= 0) { // exit(1); // } if (chdir("/") != 0) { exit(1); } // if ((dir = opendir(".")) == NULL) { // exit(1); // } //redicrect stdout if (dup2(out_log = open("PATH_TO_OUT_FILE", O_WRONLY | O_TRUNC), 1) != 1) { exit(1); } if (close(out_log) != 0) { exit(1); } //redirect stderr if (dup2(err_log = open("PATH_TO_ERR_FILE", O_WRONLY | O_TRUNC), 2) != 2) { exit(1); } if (close(err_log) != 0) { exit(1); } //redirect stdin if (dup2(conf_in = open("PATH_TO_IN_FILE", O_RDONLY), 0) < 0) { exit(1); } if (close(conf_in) != 0) { exit(1); } while ((read = getline(&line, &len, stdin)) != -1) { array_size++; printf("retrieving line of size %zd.\n", read); printf("%s\n", line); } rewind(stdin); char *path_array[array_size]; for (int i = 0; i < array_size; i++) { getline(&line, &len, stdin); // HERE IS WHERE IT GOES WRONG path_array[i] = line; } free(line); for (int i = 0; i < array_size; i++) { printf("\n\n\nretrieving line from array: %s\n", path_array[i]); } // while (1) { // puts("test output"); // printf("%zd\n", read); // // fflush(stdout); // // sleep(1); // } } #pragma clang diagnostic pop ...

    为什么会这样?我该如何阻止这种情况发生?

    这是C代码:

    var dynamicData = 0;
    setInterval(()=>{
        dynamicData = (Math.random() * 100).toFixed(0); // ???
    }, 3000);
    
    var Hello = React.createClass({
      render: function() {
        return <h2>{this.props.test}</h2>;
      }
    });
    
    ReactDOM.render(
      <Hello test={dynamicData} />,
      document.getElementById('container')
    );

1 个答案:

答案 0 :(得分:1)

在第一个调用行上为null,因此分配了一个新缓冲区。之后,line不为null,因此使用相同的缓冲区。你的数组充满了指向同一缓冲区的指针。