void IspisMatriceUDatoteku(int red, int stupac, int *matrica)
{
FILE *f;
char imeDatoteke[50];
printf("\nUnesite ime datoteke :");
scanf("%s", imeDatoteke);
f = fopen(imeDatoteke, "w");
if(NULL == f)
printf("Nevalja!!!\n");
else
{
for(int i=0; i<red; i++)
{
for(int j=0; j<stupac; j++)
{
fflush(f);
fprintf(f, "%d ", matrica[i*stupac+j]);
}
fprintf(f ,"\n");
}
}
}
int main()
{
int red, stupac;
int *a=NULL;
printf("Unesite dimenzije matrice :");//matrix dimensions rows and columns
scanf("%d %d", &red, &stupac);
a = (int*)malloc(red* stupac* sizeof(int));
IspisMatriceUDatoteku(red, stupac, a);
}
我正在尝试将矩阵写入文件。如果我尝试输入:
9 8 8
6 1 8
4 3 8
使用此代码进入文件,我得到:
9 8 8
6 1 8
4 3
所以我的问题是如何使用这样的函数将最后一个元素放入我的文件中,或者有另一种方法可以在其中写入矩阵。矩阵是在另一个函数中随机生成的。谢谢。
最佳答案
很可能是因为您没有关闭文件。将您的flush()移到最后,或者不打扰并在完成后关闭文件。
关于c - 使用C中的函数将二维数组写入文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30804411/