c++如何输出txt文件

如何把一个int数组输出到一个txt文件里,最好有简单程序代码
2024-11-08 14:44:07
推荐回答(3个)
回答1:

#include
#include
using namespace std;
int main()
{
double x,y,z;
int m;
ofstream outfile("d:/a/f1.txt",ios::in);
if(!outfile)
{
cerr<<"open f1.txt error!\n";
return 0;
}
cout<<"The first number is ";
cin>> x;
outfile<cout<<"The second number is ";
cin>>y;
outfile<cout<<"The third number is ";
cin>> z;
outfile<m=(x+y+z)/3+0.5;
outfile<cout<<"The average number is "<outfile.close();

return 0;

}

回答2:

CString struser;
CStdioFile file;
if( !file.Open(".\\data.ini", CFile::modeCreate|CFile::modeWrite) )
{
return;
}
file.WriteString(struser);
file.WriteString("\n");
file.Close();

回答3:

C++实现

#include
#include
using namespace std;

int main( int argc, char** argv )
{
ofstream
poi1,poi2;

poi1.open("point_O.txt",ios::out|ios::app);

poi1.setf(ios::fixed, ios::floatfield);

poi1.setf(ios::showpoint);

poi2.open("point_T.txt",ios::out|ios::app);

poi2.setf(ios::fixed, ios::floatfield);

poi2.setf(ios::showpoint);

poi1<<
matches[i].first.x
<< "\t"
<<
matches[i].first.y
<< endl;

poi2<< matches[i].second.x
<< "\t"
<< matches[i].second.y
<< endl;

poi1.close();

poi2.close();

std::cout<<
"Matches: " << matches.size(); //
输出数据到显示终端

}

//
注意如果在后缀为C的源文件上,采用C++输出方法来输出数据到txt文件,VS2008编译器可能报错。解决方法是将后缀改为CPP,并另建一个新的工
程,然后采用c++的输出方法,这样比较方便些。如果C文件中使用了一些非法关键字,那么修改这些变量的定义,以免与c++的关键字重复定义。