c语言的文件读写分为很多种,详细操作可参考 http://blog.csdn.net/jiji262/article/details/1835971
由于近期频繁程序生成数据进行读写操作,便于查找方便,将常用代码记录下来。
1. 格式化文件读写,经常使用在需要保存格式化的数据(比如图像矩阵数据)以及查找表操作等场合。
示例代码如下:
int OperateFile()
{
// ***********C语言写CSV文件操作测试****************
char filename[255];
sprintf(filename,"d:/writefile.csv");
FILE *wfile;
if ((wfile=fopen(filename,"w+"))==NULL)
{
printf("can not new the file\n");
exit(0);
}
//把512*512大小数据存储为CSV文件,以矩阵形式排列
for (int rows =0; rows < 512; rows++)
{
for (int cols = 0;cols < 512; cols ++)
{
fprintf(wfile,"%d,",rows + cols);
}
fprintf(wfile,"\n");//换行
}
f