13.01.2015 Views

Объектно-ориентированное программирование на С++ - eDrive

Объектно-ориентированное программирование на С++ - eDrive

Объектно-ориентированное программирование на С++ - eDrive

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

void Files2 () {<br />

AnsiString from = MyForm–>Edit1–>Text, to = MyForm–>Edit2–>Text;<br />

}<br />

try {<br />

int iFile = FileOpen (from, fmOpenRead);<br />

int len = FileSeek (iFile, 0, 2);<br />

FileSeek (iFile, 0, 0);<br />

char *c;<br />

c = new char [len + 1];<br />

len = FileRead (iFile, c, len);<br />

FileClose (iFile);<br />

iFile = FileOpen (to, fmOpenWrite);<br />

FileWrite (iFile, c, len);<br />

ShowMessage (from + " –> " + to + '\n' + AnsiString (c, len));<br />

delete [] c;<br />

FileClose (iFile);<br />

} catch (...) { ShowMessage ("Ошибка одной из файловых операций"); }<br />

69<br />

А<strong>на</strong>логичный пример посвящен отображению содержания файла в<br />

<strong>на</strong>дписи Label1:<br />

void _ _fastcall TForm1::LabelFromFileClick (TObject *Sender) {<br />

int f = FileOpen ("tempFile.txt", fmOpenRead);<br />

if (f != –1) {<br />

int size = FileSeek (f, 0, 2);<br />

char *s = new char[size+1];<br />

FileSeek (f, 0, 0);<br />

FileRead (f, s, size);<br />

Label1–>Caption = AnsiString (s, size);<br />

FileClose (f);<br />

delete [] s;<br />

}<br />

}<br />

Далее показано, как файл с текстом, указанным в Label1, дополняется<br />

содержимым поля Edit1:<br />

void _ _fastcall TForm1::InsertToFileClick (TObject *Sender) {<br />

int f = FileOpen ("tempFile.txt", fmOpenWrite);<br />

if (f != –1) {<br />

AnsiString is = Label1–>Caption, what = Edit1–>Text;<br />

is += what;<br />

Label2–>Caption = is;<br />

FileWrite (f, is.c_str (), is.Length ());

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!