1.利用FILE写数据
#include <stdio.h>
#include <iostream>
using namespace std;
int main()
{
printf("请输入数据");
int m,n;
n =0;
while (cin >> m)
{
n += m;
}
FILE *fp;
fp = fopen("han.txt","wb"); //打开
fprintf(fp,"%d",n); //存储数据n
fclose(fp); //关闭
return 0;
}
//当存Mat矩阵数据时,(或数组时)
void main()
{
Mat mat( 640, 640, CV_32FC2, cv::Scalar::all(0) ) ;
FILE *fp ;
fp = fopen( "wendanh.txt", "wb" ) ;
for(int i=0; i<my_mat.rows; i++)
{
for(int j=0; j<my_mat.cols; j++)
{
fprintf(fp, "%3.0f", my_mat.at<float>(i,j) ) ; // the data type should be matched.
}
fprintf( fp, "\n" ) ;
}
fclose( fp ) ;
return 1 ;
}
2.利用ofstream写数据
注意添加头文件, #include <fstream>
int main()
{
printf("请输入数据");
int m,n;
n =0;
while (cin >> m)
{
n += m;
}
ofstream wof( fileName.c_str() ) ;
wof << n ;
wof .close() ;
return 0;
}