按陈良乔的书写测试的。
我觉得先写入,再读出比较好,
所以修改了一下执行顺序。。。。
代码:
#include <cstdlib> #include <iostream> #include <fstream> using namespace std; int main(int argc, char *argv[]) { int nYear, nMonth, nDate; cout<<"please input today(year month date):"<<endl; cin>>nYear>>nMonth>>nDate; ofstream fout("Date.txt"); if( !fout.bad()) { fout<<"the day user input is: \n" <<nYear<<" "<<nMonth<<" "<<nDate; fout.close(); } else { cout<<"cann't open and write this file."<<endl; } ifstream fin("Date.txt"); if( !fin.bad()) { fin.ignore(256,'\n'); fin>>nYear>>nMonth>>nDate; cout<<"date in the file is :"<<nYear<<"-"<<nMonth<<"-"<<nDate<<endl; fin.close(); } else { cout<<"cann't open this file."<<endl; } return 0; system("PAUSE"); return EXIT_SUCCESS; }
运行:
时间: 2024-09-27 02:33:52