终于写了一些有用的代码,第一个是执行在999内挑选出水仙花数,并存入文件里,第二个是读取文件,显示内容。
void out_file(){ int k=100,l,m,n,count=0; ofstream ofile; ofile.open("myfile.txt"); ofile<<"水仙花数有:"<<endl; do{ l=k/100; n=k%10; m=(k-l*100-n)/10; if(k==l*l*l+m*m*m+n*n*n){ ofile<<l<<"的3次方+"<<m<<"的3次方+"<<m<<"的3次方="<<k<<'/n'; count++; if(count%5==0)ofile<<endl; } k++; }while(k<9999); ofile<<endl; ofile.close(); } void in_file(){ char ch[256]; ifstream ifile; ifile.open("myfile.txt"); cout<<"文件内容:"<<endl; do{ ifile.getline(ch,255); cout<<ch<<endl; }while(ifile.eof()==0); ifile.close(); }