读写文件(fstream)

// 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;
}
这里是将写入的东西覆盖了以前的文字,所希望的是在以前的基础上加上现在输入的文字。

你可能感兴趣的:(读写文件(fstream))