C语言二维数组文件输入输出

来源:百度知道 编辑:UC知道 时间:2024/06/19 20:36:40
我自己编写了个程序,是对二维数组的一个操作,程序的开始需要从文件(txt文件)读取数据到二维数组中,程序的最后把处理好的数据重新建立一个文件输出。
数据的格式:
1 2 3
4 5 6
...
数据之间的空位一个TAB键大小。
请为我写一个输入和最后输出的程序

#include <stdio.h>

void ReadFromFile(char *filename, int **a, int row, int col)
{
int i,j;
FILE *fp = NULL;

if(!filename)
{
printf("read file,parameter error NULL\n");
return;
}

fp = fopen(filename,"r");
if(!fp)
{
printf("read file, can't open the file:%s\n",filename);
return;
}

for( i = 0; i < row; i++)
{
for(j = 0; j> col;j++)
{
fscanf(fp,"%d\t",&a[i][j]);
}
}

fclose(fp);
}

void writetofile(char *filename, int **a, int row, int col)
{
int i,j;
FILE *fp = NULL;

if(!filename)
{
printf("write file,parameter error NULL\n");
return;
}

fp = fopen(filename,"r");
if(!fp)
{
printf("write file, can't open th