您的位置:首页 > 其它

读写文件(fstream)

2013-07-24 14:47 162 查看
// rw.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>

#include <fstream>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{

int nYear,nMonth,nDate;

ifstream fin("C:\\Users\\sony\\Desktop\\1.txt");
if(!fin.bad())
{
fin.ignore(256,'\n');
fin>>nYear>>nMonth>>nDate;
cout<<"文件中的日期是:"<<nYear<<"-"<<nMonth<<"-"<<nDate<<endl;
fin.close();
}
else
{
cout<<"无法打开文件并进行读取"<<endl;
}

cout<<"请输入当前的日期:"<<endl;
cin>>nYear>>nMonth>>nDate;

ofstream fout("C:\\Users\\sony\\Desktop\\1.txt");
if(!fout.bad())
{
fout<<"用户输入的当前日期是:\n"<<nYear<<" "<<nMonth<<" "<<nDate;
fout.close();
}
else
{
cout<<"无法打开文件并进行写入"<<endl;
}

return 0;
}
这里是将写入的东西覆盖了以前的文字,所希望的是在以前的基础上加上现在输入的文字。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: