c++如何输出txt文件

如何把一个int数组输出到一个txt文件里,最好有简单程序代码

第1个回答  推荐于2017-10-01
#include<iostream>
#include<fstream>
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<<x<<endl;
cout<<"The second number is ";
cin>>y;
outfile<<y<<endl;;
cout<<"The third number is ";
cin>> z;
outfile<<z<<endl;
m=(x+y+z)/3+0.5;
outfile<<m<<endl;
cout<<"The average number is "<<m<<endl;
outfile.close();

return 0;

}追问

复制挺快的嘛;
by the way ,thanks

本回答被提问者采纳
第2个回答  2011-09-20
CString struser;
CStdioFile file;
if( !file.Open(".\\data.ini", CFile::modeCreate|CFile::modeWrite) )
{
return;
}
file.WriteString(struser);
file.WriteString("\n");
file.Close();追问

".\\data.ini"这是什么格式的,什么意思

追答

ini可以直接改后缀为txt,都是文本文件

追问

需要什么头文件呢

追答

#include
#include

追问

Cstring未知变量

追答

汗,我这个是MFC的代码

追问

Cstring类不用导入的吗

追答

你用别的类吧,用string,或者char

第3个回答  推荐于2017-09-10
C++实现

#include <iostream>
#include <fstream>
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++的关键字重复定义。
相似回答