【问题标题】:Line from file isn't reading after using pointer inside the loop在循环内使用指针后未读取文件中的行
【发布时间】:2017-09-12 17:08:42
【问题描述】:

所以我正在尝试从文件中读取矩阵(我确信有比我这样做更好的方法来做到这一点)。我很难弄清楚如何从文件中读取每个单词(即矩阵的每个条目),因此决定读取每一行并使用我在 stackexchange 中找到的名为 strtok 的东西。

我的main()内部是这样的

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <malloc.h>
#include <time.h>
#include <string.h>

int main(){
    FILE *f;
    int nmatrix=3;
    int nmax=3;
    int something;
    int number, count, count2, i, j;
    srand(time(NULL));
    size_t len = 0;
    ssize_t read;
    char * line = NULL;
    char * pch;
    int ia;
    int (*B)[nmatrix][nmatrix] = malloc(nmatrix * nmatrix * sizeof(int));


    while(nmatrix<=nmax){
        // Creation of Matrix
        f = fopen("matrix.txt", "w");
        for(count = 1; count <= nmatrix; count ++){
            for(count2 = 1; count2 <= nmatrix; count2 ++){
                number = rand()%9;
                fprintf(f, "%s%d ", " ", number);
            }
            fprintf(f, "%s\n", " ");
        }
        fclose(f);

        // Reading Matrix
        f = fopen("matrix.txt", "r");
        i=0;
        while((read = getline(&line, &len, f)) != -1) {
            printf("%s\n", line);
            pch = strtok(line," ,.-");
            j=0;
            while (pch != NULL & j<nmatrix){
                ia= (int)*pch-48;
                *B[i][j]= ia;
                pch = strtok(NULL, " ,.-");
                j=j+1;
            }
            i=i+1;
        }
        fclose(f);

        nmatrix=nmatrix+1;
    }


    return 0;
}

终端中的第一个输出是*B[i][j]= ia; 行是否被擦除,第二个输出是它。第一个输出读取文件中的所有行,第二个不读取最后一个。为什么? (输出看起来不同,因为矩阵是随机生成的)。

提前致谢

我对一切都很陌生,特别是指针,所以如果没有正确使用任何东西,我会很感激评论。

【问题讨论】:

  • 请提供minimal reproducible example。虽然我很高兴看到初学者使用正确的矩阵类型:2D 数组,但是如果您使用指向 1D 数组的指针,您可以让您的生活更轻松。我有时已经回答过这样的问题,你可能想查看我的个人资料::answers。
  • 你没有为line分配memory这就是程序崩溃的原因,首先分配内存来存储line中的string
  • 好的,我现在把整个代码放好,至少现在如果有人想运行它,他们不必创建文件(代码创建它)。
  • @krpra,你为什么说没有错误就崩溃了?不知道你在说什么,我首先必须分配内存来存储字符串,请你说得更具体些。
  • @krpra 不,请RTF getline() M 如果*lineptr 是空指针或*lineptr 指向的对象大小不足,则应分配对象malloc() 或对象应被重新分配,就像分别由 realloc() 一样,使得对象足够大以容纳要写入它的字符

标签: c file


【解决方案1】:

你有几个问题。您使用的是 & 而不是 && (并没有真正影响它,但仍然是错误的)并且您在作业中的指针错误。我更改了输出以显示存储在矩阵中的内容,而不是读取行。 *B[i][j]= ia; 必须是(*B)[i][j]= ia;

   // Reading Matrix
    f = fopen("matrix.txt", "r");
    i=0;
    while((read = getline(&line, &len, f)) != -1) {
        pch = strtok(line," ,.-");
        j=0;
        while (pch != NULL && j<nmatrix){
            printf("\n%d %d :", i, j);
            ia= (int)*pch-48;
            (*B)[i][j]= ia;
            printf("%d\n", (*B)[i][j]);
            pch = strtok(NULL," ,.-");
            j=j+1;
        }
        i=i+1;
    }
    fclose(f);

【讨论】:

    【解决方案2】:

    你有很多错误:

    • 当您创建矩阵时,不会插入用于破坏矩阵数字的字符串:“,.-”。这些必须插入才能在 strtok 函数中使用。
    • 在 while 语句中应该是 && 而不是 &
    • 文件末尾有一个额外的 }。
    • 创建矩阵的两个时间都应从 0 开始,然后使用
    • 你应该用“”初始化字符串行,而不是空。

      int main(){

      FILE *f;
      int nmatrix=3;
      int nmax=3;
      int number, count, count2, i, j;
      srand(time(NULL));
      size_t len = 0;
      ssize_t read;
      char * line = "";
      char * pch;
      int ia;
      int (*B)[nmatrix][nmatrix] = malloc(nmatrix * nmatrix * sizeof(int));
      while(nmatrix<=nmax){
          // Creation of Matrix
          f = fopen("matrix.txt", "w");
          for(count = 0; count < nmatrix; count ++){
              for(count2 = 0; count2 < nmatrix; count2 ++){
                  number = rand()%9;
                  fprintf(f, "%s%d ", " ", number);
              }
              fprintf(f, "%s\n", " ");
          }
          fclose(f);
      
          // Reading Matrix
          f = fopen("matrix.txt", "r");
          i=0;
          while((read = getline(&line, &len, f)) != -1) {
              printf("%s\n", line);
              pch = strtok(line," ");
              j=0;
              while (pch != NULL && j < nmatrix){
                  ia= (int)*pch-48;
                  *B[i][j]= ia;
                  pch = strtok(NULL, " ");
                  j=j+1;
              }
              i=i+1;
          }
          fclose(f);
      
          nmatrix=nmatrix+1;
      }
      return 0;
      

      }

    【讨论】:

    • 谢谢!空格是它们的分隔方式,它位于“ ,.-”中。其他人回答说分配字符串应该修复它并建议将字符串初始化为char * line= (char *)malloc( 100 * sizeof(char));,我这样做了,没有任何改变。
    • 你应该用“”初始化字符串行,而不是null。不正确。贴出的代码中linelengetline()的使用是正确的。请参阅pubs.opengroup.org/onlinepubs/9699919799/functions/… 用于存储结果的变量的名称 - read - 可能会更好,因为它与 POSIX read() 函数冲突,但这也不是不正确的,即使它可能在某些方面令人困惑。
    • 如果你用空格作为分隔符,你必须在strtok函数中使用“”,而不是“,.-”。我在这里测试过,效果很好。
    猜你喜欢
    • 2016-08-12
    • 1970-01-01
    • 2012-05-21
    • 1970-01-01
    • 1970-01-01
    • 2018-07-27
    • 2018-04-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多