C++ primer plus第六版课后编程题答案17.2


main172.cpp

#include <fstream>
#include <iostream>
#include <string>
using namespace std;
void main172()
{
    ofstream fout;
    cout<<"Enter the filename.txt:";
    char filename[50];//用char 
   	cin>>filename;
	fout.open(filename);
    while(fout.fail())
    {
        cout<<"wrong!"<<endl;
		cout<<"Enter the filename.txt:"; 
		cin>>filename;
		fout.open(filename);
       }
       
    cout<<"Enter the str:";  
    string str;  
    while(cin>>str)//&&!fout.fail())
	//while(!fout.eof())不知道为什么,不能正确处理文件尾
    {
         //cin>>str;
		//getline(cin,str);
         fout<<str;                   
         cout<<"Enter the str:";          
    }
    fout.close();
    
    
}

 

 


发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注