C 文件中的行是';在循环内使用指针后读取

C 文件中的行是';在循环内使用指针后读取,c,file,C,File,所以我试图从一个文件中读取矩阵(我确信有比我现在做的更好的方法)。我很难理解如何读取文件中的每个单词(表示矩阵的每个条目),因此决定读取每一行,并使用我在stackexchange中找到的名为strtok的东西 我的main()的内部如下所示 #include <stdio.h> #include <stdlib.h> #include <stdbool.h> #include <string.h> #include <malloc.h>

所以我试图从一个文件中读取矩阵(我确信有比我现在做的更好的方法)。我很难理解如何读取文件中的每个单词(表示矩阵的每个条目),因此决定读取每一行,并使用我在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;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
int main(){
文件*f;
int nmatrix=3;
int nmax=3;
智力的东西;
整数,计数,计数2,i,j;
srand(时间(空));
尺寸长度=0;
阅读;
char*line=NULL;
char*pch;
INTIA;
int(*B)[nmatrix][nmatrix]=malloc(nmatrix*nmatrix*sizeof(int));

而(nmatrix您有很多错误:

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

    int main(){

    文件*f;
    int nmatrix=3;
    int nmax=3;
    整数,计数,计数2,i,j;
    srand(时间(空));
    尺寸长度=0;
    阅读;
    字符*行=”;
    char*pch;
    INTIA;
    int(*B)[nmatrix][nmatrix]=malloc(nmatrix*nmatrix*sizeof(int));
    
    而(nmatrix您遇到了一些问题。您使用了&而不是&&(没有真正影响它,但仍然是错误的),并且您的指针在赋值中出错。我更改了输出以显示存储在矩阵中的内容,而不是读取行。
    *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);
    
    //读取矩阵
    f=fopen(“matrix.txt”,“r”);
    i=0;
    while((read=getline(&line,&len,f))!=-1){
    pch=strtok(行“,.-”);
    j=0;
    
    虽然(pch!=NULL&&j请提供一个。虽然我很高兴看到初学者使用正确的矩阵类型:2D数组,但是如果您使用指向1D数组的指针,您的生活会更轻松。我有时已经回答了这样的问题,您可能想检查我的配置文件::答案。您没有为
    分配
    内存
    这就是程序崩溃的原因,首先分配内存将
    字符串存储在
    中。好的,我现在就把整个代码放进去,至少现在如果有人想运行它,他们不必创建文件(代码创建它)@krpra,如果没有错误,为什么说它崩溃了?我不确定你说的是什么,我首先要分配内存来存储字符串,你能说得更具体些吗。@krpra没有。如果
    *lineptr
    是空指针,或者
    *lineptr
    指向的对象大小不足,请分配一个对象通过
    malloc()
    进行ted,或者分别通过
    realloc()
    重新分配对象,以使对象足够大,可以容纳要写入它的字符!空格是它们的分隔方式,以“,”表示。其他人回答说分配字符串应该修复它,并建议将字符串初始化为
    char*line=(char*)malloc(100*sizeof(char));
    我这样做了,但没有做任何更改。您应该使用“”初始化字符串行,不为null。不正确。
    line
    len
    getline()发布的代码中的
    是正确的。请查看用于存储结果的变量的名称-
    read
    -可能会更好,因为它与POSIX
    read()
    函数冲突,但这也不是错误的,即使在某些方面可能会混淆。如果空格是用作分隔符的,则必须使用“”在strtok函数中,不是“,.-”。我在这里进行了测试,工作正常。
       // 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);