代码如下,一个生成可逆矩阵的子函数,第一个fopen函数里如果用“w”或者“w+”打开就会出错而如代码中所示用“a+”打开就不会出错,求知道求鞭策。。。
int matrix()/* 生成可逆矩阵 */
{
int det,i,j;
int **mat;
FILE *fp;
if((fp=fopen("mat.txt","a+")) == NULL)
{
printf("\nFailed to open the file.\n");
exit(1);
}
srand(time(NULL));
mat = (int **)malloc(ORDER_M * sizeof(int *));
for(i = 0;i < ORDER_M;i++)
mat[i] = (int *)malloc(ORDER_M * sizeof(int));
for(i = 0;i < ORDER_M;i++)
{
for(j = 0;j < ORDER_M;j++)
{
mat[i][j] = rand() % MOD_M;
fprintf(fp,"%4d",mat[i][j]);
}fprintf(fp,"\n");
}
det = detmat(mat,ORDER_M);
if(det % 2 == 0||det % 13 == 0||det == 0)
status = 0;
else
status = 1;
fclose(fp);
while(!status)/* 判断矩阵是否可逆,不可逆则重新随机生成一个矩阵,并进行再判断 */
det=matrix();
for(i = 0;i < ORDER_M;i++)
free(*(mat+i));
free(mat);
return det;
}
"w"
Opens an empty file for writing. If the given file exists, its contents are destroyed.
"w+"
Opens an empty file for both reading and writing. If the given file exists, its contents are destroyed.
"a+"
Opens for reading and appending; the appending operation includes the removal of the EOF marker before new data is written to the file and the EOF marker is restored after writing is complete; creates the file first if it doesn’t exist.
出什么错?你在你指定的目录上有权限没有?