Здравствуйте, Людмила Хисматулина.
Полный проект здесь
177166.ZIP (546.9 кб)Файл imya.txt должен располагаться в каталоге исполняемого файла программы. Модуль Unit1.cpp в приложении. Поскольку ничего не говорится о проверке чисел в файле imya.txt, она и не производится.
Приложение:
//---------------------------------------------------------------------------
#include <vcl.h>
#include <iostream>
#include <fstream>
#include <string>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
using namespace std;
ifstream in; // исходный файл с данными
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btStartClick(TObject *Sender)
{
static bool finished = false; // просмотрены все строки исходного файла?
const char* FileName = "imya.txt";
string s; // считываем строку исходного файла
AnsiString msg; // сообщение
if (finished)
{
ShowMessage("Все строки просмотрены");
return;
}
if (!in.is_open()) // если исходный файл еще не открыт, откроем его
{
in.open(FileName);
if (!in)
{
msg = "Не удалось открыть файл " + AnsiString(FileName);
ShowMessage(msg);
return;
}
}
if (!in.eof())
{
in >> s; // прочитали строку исходного файла
s+=".txt"; // имя вновь создаваемого файла
ofstream out(s.c_str(),ios::in);
if (!out) // если файл еще не существует
{
msg = "Создан файл "+AnsiString(s.c_str());
out.clear();
out.open(s.c_str(), ios::out);
ShowMessage(msg);
out << Edit1->Text.c_str() << endl;
out.close();
}
else
{
ShowMessage("Файл не создан!");
out.close();
}
if (in.eof())
{
finished = true;
in.close();
}
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
{
if (in.is_open()) // закроем исходный файл
{
in.close();
}
}
//---------------------------------------------------------------------------