首页 新闻 会员 周边

关于c语言fopen函数的问题

0
悬赏园豆:20 [待解决问题]

代码如下,一个生成可逆矩阵的子函数,第一个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;
}

天随子的主页 天随子 | 初学一级 | 园豆:180
提问于:2011-10-10 20:07
< >
分享
所有回答(2)
0

"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.

胡健 | 园豆:215 (菜鸟二级) | 2012-02-24 20:30
0

出什么错?你在你指定的目录上有权限没有?

荣耀属于跪拜猫 | 园豆:832 (小虾三级) | 2012-10-07 10:29
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册