#include <iostream>
#include <fstream>
#include <string>
using namespace std;
//чтение из файла
void fileDisplay(char* path)
{
ifstream iFSTR;
iFSTR.open(path);
char buffer[80];
if(iFSTR.is_open())
{ while (iFSTR.good())
{ iFSTR.getline(buffer,80,'\n');
cout<<buffer<<"\n";
}
iFSTR.close();
}
else
{ cout<<"Error opening file\n";
cin.get();
exit(1);
}
}
//поиск длины первого самого короткого слова
int shortest(char* path)
{ int N, min=100;
string current, change="";
ifstream iFSTR;
iFSTR.open(path);
if(iFSTR.is_open())
{ while(iFSTR.good())
{ iFSTR>>current; //считывание отдельных слов
N = current.length();
if (N < min) min = N;
}
iFSTR.close();
return min;
}
else
{ cout<<"Error opening file\n";
cin.get();
exit(1);
}
}
//замена слова на ** с записью в файл
void changeToStars(char* path, int minL)
{ long position;
string current, change = "";
for(int i = 0; i<minL;i++)
change+="*"; //строка минимальной длины из **
fstream FSTR;
FSTR.open(path,fstream::in | fstream::out);
if(FSTR.is_open())
{ while(FSTR.good())
{ FSTR>>current;//считывание отдельных слов
if(current.length()==minL)
{ position = FSTR.tellg();
FSTR.seekp(position-minL);//возврат указателя на начало слова
FSTR<<change;//замена на строку из **
break;
}
}
FSTR.close();
}
else
{ cout<<"Error opening file\n";
cin.get();
exit(1);
}
}
int main ()
{
char* pathF = "test.txt";
cout<<"Original file\n";
cout<<"______________________________________________\n";
fileDisplay(pathF);
int found = shortest(pathF);
changeToStars(pathF,found);
cout<<"\n\nChanged file\n";
cout<<"______________________________________________\n";
fileDisplay(pathF);
cin.get();
return 0;
}
|