#include <iostream>
#include <fstream>
#include <string>
using namespace std;
class File
{
string name;
fstream fs;
public:
File( string name1):name(name1){};
bool open(int mode=0);// 0 - read, else - write
fstream &get_stream() {return fs;}
string get_name() {return name;}
~File() {fs.clear(); fs.close(); }
};
bool File::open(int mode)
{
if(mode==0)
fs.open(name.c_str(),ios::in);
else
fs.open(name.c_str(),ios::out);
if(!fs)
{
return false;
}
return true;
}
class Str
{
string str;
string max_str;
unsigned max_len;
public:
Str():max_len(0),str(""),max_str(""){}
string read_file_find_max(fstream &f);
void write_file(fstream &f);
};
string Str::read_file_find_max(fstream &f)
{
while(!f.eof())
{
getline(f,str);
if(str.length() > max_len)
{
max_len=str.length();
max_str=str;
}
f>>ws;
}
return max_str;
}
void Str::write_file(fstream &f)
{
string s;
while(1)
{
getline(cin,s);
if(s=="") // ввели просто Enter- закончили ввод
break;
f<<s;
f<<endl;
}
}
int main()
{
File *file;
Str *s;
string m;
// читаем из файла
file=new File("f.txt");
file->open();
s= new Str;
m = s->read_file_find_max(file->get_stream());
cout << m << endl;
delete file;
delete s;
// пишем в файл
file = new File("f2.txt");
file->open(1);
s= new Str;
s->write_file(file->get_stream());
delete file;
delete s;
//читаем из созданного файла
file=new File("f2.txt");
file->open();
s= new Str;
m = s->read_file_find_max(file->get_stream());
cout << m << endl;
delete file;
delete s;
}
/*
f.txt
sdfsdf fgedf
ssdf
sdfffffffff fgfgff
--------------
f2.txt (тот, что ввели с клавиатуры)
123 rtrt
11111111111112 455555
sdd
fggf fg
*/
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8ZnN0cmVhbT4KI2luY2x1ZGUgPHN0cmluZz4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKY2xhc3MgRmlsZQp7CglzdHJpbmcgbmFtZTsKCWZzdHJlYW0gZnM7CnB1YmxpYzoKCUZpbGUoIHN0cmluZyBuYW1lMSk6bmFtZShuYW1lMSl7fTsKCWJvb2wgb3BlbihpbnQgbW9kZT0wKTsvLyAwI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