无法在 C 上的 FILE 上写入二维数组

Cant write a 2d array on a FILE on C

char arrTypeLabels[3][7]= {{"Random"},{"ASC"},{"DESC"}};
FILE *f;
    f= fopen( "TIMES.txt", "wb");
    if (f == NULL)
    {
    printf("Error! Could not open file\n");
    exit(-1);
    }
    int i,j;
    for(i=0;i<3;i++)
    {
    for(j=0;j<7;j++)
    {
    printf("%c",arrTypeLabels[i][j]);
    fwrite(arrTypeLabels[i][j],sizeof(char),sizeof(arrTypeLabels),f);   
    }
    }
    fclose(f);aenter code here

Im opening the TIMES.txt file but i cant see any output, althought i think my code is right .......................... :/ pls help...

char arrTypeLabels[3][7] = {
    {"Random"},
    {"ASC"},
    {"DESC"}
};
FILE *f = fopen("TIMES.txt", "wb"); //wb is OK
if (f == NULL)
{
    printf("Error! Could not open file\n");
    exit(-1);
}
int i, j;
for (i = 0; i < 3; i++)
{
    for (j = 0; j < 7; j++)
    {
        printf("%c", arrTypeLabels[i][j]);
        fwrite(arrTypeLabels[i] + j, sizeof (char), sizeof (char), f);  //your mistake is here
    }
}
fclose(f);

我什至不知道您是如何编译代码的,因为在 fwrite 中,第一个参数需要是一个指针,或者在您的代码中,您要提供值。
此外,您尝试做的事情令人困惑,因为看起来您正试图通过 char 编写 char,但您正试图写入 arrTypeLabels 中包含的全部数据] 在一次 fwrite 通话中。

fwrite第一个和第三个参数错误。因为你想一个字符一个字符地写,你的行应该是 fwrite(&buf[i][j], 1,1,f);

或者更简单,使用fputc:

 fputc(buf[i][j], f);

如果你只是想将该数组写入文件,你可以这样写:

char arrTypeLabels[3][7]= {{"Random"},{"ASC"},{"DESC"}};
    FILE *f;
    f= fopen( "TIMES.txt", "wb");
    if (f == NULL)
    {
        printf("Error! Could not open file\n");
        exit(-1);
    }
    fwrite(arrTypeLabels,sizeof(char),sizeof(arrTypeLabels),f);
    fclose(f);

或类似的东西:

char arrTypeLabels[3][7]= {{"Random"},{"ASC"},{"DESC"}};
    FILE *f;
    f= fopen( "TIMES.txt", "wb");
    if (f == NULL)
    {
        printf("Error! Could not open file\n");
        exit(-1);
    }
    for (int i = 0; i < 3; i++)
    {
        for (int j = 0; j < 7; j++)
            fwrite(arrTypeLabels[i] + j,sizeof(char),sizeof(char),f);
    }
    fclose(f);

请注意,第一种方法要快得多,因为您不必一次写入(写入文件,即写入硬盘)每个字符。