问题描述
- 关于C++文件输出到.txt中的问题
-
#include
using namespace std;
class canlendar
{
private:
int year,weekday,month;
public:
int firstday(int year);
void printmonthday(int month);
int monthday(int month);
void printmonth(int month);
};
int firstday(int year)
{
int s;
year=year-1;
s=year+year/4-year/100+year/400+1;
s=s%7;
return s;
}
void printmonthhead(int month)
{
cout<<" "<<month<<"月"<<endl<<endl;
cout<<" 日 一 二 三 四 五 六"<<endl;
}
int monthday(int month)
{
int a;
int year;
switch(month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:a=31;break;
case 4:
case 6:
case 9:
case 11:a=30;break;
case 2:if(year%4==0||year%400==0&&year%100!=0)
a=29;
else
a=28;break;
}
return a;
}
void printmonth(int month)
{
int a,b;
int weekday;
printmonthhead(month);
b=monthday(month);
for(int c=-1;c<=weekday;c++)
cout<<' ';
for(a=1;a<=b;a++)
{
cout<<a<<' ';
if(weekday==6)
{
cout<<endl<<" ";
weekday=weekday-7;
}
weekday++;
}
}int main()
{
int year,weekday;
cout<<"请输入年份:"<
cin>>year;
if(year<=0)
{
cout<<"年份错误!"<<endl;
return -1;
}
else
{
weekday=firstday(year);
cout<<" "<<year<<"年历"<<endl<<endl;
for(int month=1;month<=12;month++)
{
printmonth(month);
cout<<endl<<endl;
}
}
return 0;
}想把这个输出结果输入一个文件中,求大神指点迷津
解决方案
关于vs2010 中c++输出字符串到txt文件中的格式问题
C++处理txt文件
解决方案二:
ofstream outfile("filename.txt",ios::binary);
if(!outfile)
{
cerr<<"open error!"<<endl;
return 0;
}
outfile.write(..........);//写数据到文件中
outfile.close();//关闭流
头文件fstream.h
解决方案三:
可以直接用 freopen("文件的路径",“w”, stdout);
时间: 2025-01-27 20:02:37