Unit1.cpp
上传用户:lhxd_sz
上传日期:2014-10-02
资源大小:38814k
文件大小:3k
源码类别:

VC书籍

开发平台:

C++ Builder

  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #include <io.h>
  4. #pragma hdrstop
  5. #include "Unit1.h"
  6. //---------------------------------------------------------------------------
  7. #pragma package(smart_init)
  8. #pragma resource "*.dfm"
  9. TForm1 *Form1;
  10. //---------------------------------------------------------------------------
  11. __fastcall TForm1::TForm1(TComponent* Owner)
  12.         : TForm(Owner)
  13. {
  14. }
  15. //---------------------------------------------------------------------------
  16. void __fastcall TForm1::WriteString(int ff,AnsiString ss)
  17. {
  18.   AnsiString ass;
  19.   int nn;
  20.   ass=ss+"rn";
  21.   nn=ass.Length();
  22.   FileWrite(ff,ass.c_str(),nn);
  23. }
  24. //---------------------------------------------------------------------------
  25. AnsiString __fastcall TForm1::ReadString(int ff)
  26. {
  27.   AnsiString ass;
  28.   char ss[2];
  29.   int nn;
  30.   ass="";
  31.   ss[1]=0;
  32.   for(;;)
  33.   {
  34.     nn=FileRead(ff,ss,1);
  35.     if(nn<1)
  36.     {
  37.       ass="~~~END~~~";
  38.       break;
  39.     }
  40.     if((ss[0]==0x0d)||(ss[0]==0x0a))
  41.     {
  42.       if(ass!="")break;
  43.     }
  44.     else
  45.     {
  46.       ass=ass+ss;
  47.     }
  48.   }
  49.   return ass;
  50. }
  51. //---------------------------------------------------------------------------
  52. void __fastcall TForm1::Button2Click(TObject *Sender)
  53. {
  54.   int ii,ff;
  55.   if(SaveDialog1->Execute())
  56.   {
  57.     ff=FileCreate(SaveDialog1->FileName);
  58.     if(ff==-1)return;
  59.     for(ii=0;ii<Memo1->Lines->Count;ii++)
  60.     {
  61.       WriteString(ff,Memo1->Lines->Strings[ii]);
  62.     }
  63.     FileClose(ff);
  64.   }
  65. }
  66. //---------------------------------------------------------------------------
  67. void __fastcall TForm1::Button1Click(TObject *Sender)
  68. {
  69.   int ff;
  70.   AnsiString ass;
  71.   if(OpenDialog1->Execute())
  72.   {
  73.     ff=FileOpen(OpenDialog1->FileName, fmOpenRead);
  74.     if(ff==-1)return;
  75.     Memo1->Clear();
  76.     for(;;)
  77.     {
  78.       ass=ReadString(ff);
  79.       if(ass=="~~~END~~~")break;
  80.       else Memo1->Lines->Add(ass);
  81.     }
  82.     FileClose(ff);
  83.   }
  84. }
  85. //---------------------------------------------------------------------------
  86. void __fastcall TForm1::Button3Click(TObject *Sender)
  87. {
  88.   int nn,ff;
  89.   AnsiString ass;
  90.   char ss[2];
  91.   if(OpenDialog1->Execute())
  92.   {
  93.     ff=FileOpen(OpenDialog1->FileName, fmOpenRead);
  94.     if(ff==-1)return;
  95.     Memo1->Text="";
  96.     for(;;)
  97.     {
  98.       nn=FileRead(ff,ss,1);
  99.       if(nn<1)break;
  100.       Memo1->Text=Memo1->Text+ss;
  101.     }
  102.     FileClose(ff);
  103.   }
  104. }
  105. //---------------------------------------------------------------------------
  106. void __fastcall TForm1::Button4Click(TObject *Sender)
  107. {
  108.   int ii,ff,nn;
  109.   if(SaveDialog1->Execute())
  110.   {
  111.     ff=FileCreate(SaveDialog1->FileName);
  112.     if(ff==-1)return;
  113.     nn=Memo1->Text.Length();
  114.     FileWrite(ff,Memo1->Text.c_str(),nn);
  115.     FileClose(ff);
  116.   }
  117. }
  118. //---------------------------------------------------------------------------