#include "stdafx.h"
#include <stdlib.h>
void writeData(int nArray[][4],int row,int col)
{
FILE* file;
errno_t error;
error = fopen_s(&file,"e:\\XXX.txt","wb");
if (error )
{
printf("文件打开失败");
exit(EXIT_FAILURE);
}
for (int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
{
fwrite(&i,sizeof(int),1,file);
fwrite(&j,sizeof(int),1,file);
fwrite(&nArray[i][j],sizeof(int),1,file);
}
}
fclose(file);
}
void readData(int nArray[][4],int row,int col)
{
FILE* file;
errno_t error;
error = fopen_s(&file,"e:\\XXX.txt","rb");
if (error )
{
printf("文件打开失败");
exit(EXIT_FAILURE);
}
int nRow = 0;
int nCol = 0;
for (int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
{
fread(&nRow,sizeof(int),1,file);
fread(&nCol,sizeof(int),1,file);
fread(&nArray[nRow][nCol],sizeof(int),1,file);
}
}
fclose(file);
}
int _tmain(int argc, _TCHAR* argv[])
{
int nArrayA[][4] = {{1,2,3,4},{5,6,7,8},{9,10,11,12}} ;
int nArrayB[3][4];
writeData(nArrayA,3,4);
readData(nArrayB,3,4);
return 0;
}
编译环境是Visual Studio 2012.