关于C++中输出TXT格式文件的问题

来源:百度知道 编辑:UC知道 时间:2024/06/17 20:37:34
初学c++有很多不疑惑
如以下程序如何将变量x,y,z和结果m输出到txt文件中:

#include<iostream>
using namespace std;
int main()
{
double x,y,z;
int m;
FILE *p;
p=fopen("C:\\123.txt","w+");
cout<<"The first number is ";
cin>> x;
cout<<"The second number is ";
cin>> y;
cout<<"The third number is ";
cin>> z;
m=(x+y+z)/3+0.5;
cout<<"The average number is "<<m<<endl;
{
fprintf(p,);
}
}
谢谢大家!!!
谢谢!!
不应该输入“FILE *p;
p=fopen("C:\\123.txt","w+");”是么?
那修改完之后 程序应该是什么样的呢?

#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;

}

fprintf(p,"x=%lf\ny=%lf\nz=%lf\nresult=%lf\n",x,y,z,m);
不过这可不是C++,这是C语言。在C++中应该改成:
#include <fstream.h>//如果没有using name.....就得#inclu