1. Fayllar bilan ishlash. Fayllarni yuklash va saqlash. Fayllar bilan ishlash. Misol Matn taxrirlovchi dastur tuzing
Download 421.22 Kb. Pdf ko'rish
|
2-amaliy mashg\'ulot
Fayllarni yuklash va saqlash. LoadFromFile("Fayl nomi"); SaveToFile("Fayl nomi"); Fayllarni yuklash va saqlashga misollar: //Tmemo komponenti: Memo1->Lines->Clear(); Memo1->Lines->LoadFromFile("a.txt"); ...... Memo1->Lines->SaveToFile("a.txt"); ...... // TListBox1 komponenti: ListBox1->Items->Clear(); ListBox1->Items->LoadFromFile("a.txt"); ...... ListBox1->Items->SaveToFile("a.txt"); // ComboBox1 komponenti: ComboBox1->Items->Clear(); ComboBox1->Items->LoadFromFile("a.txt"); ...... ComboBox1->Items->SaveToFile("a.txt"); // TrichEdit komponenti: RichEdit1->Lines->Clear(); RichEdit1->Lines->LoadFromFile("a.txt"); ..... RichEdit1->Lines->SaveToFile("a.txt"); SaveToFile funksiyasini qo‘llaganda agar fayl mavjud bo‘lmasa u xolda yangi fayl yaratiladi. Yuqoridagi misollarda matn fayllar yaratiladi. TRichEdit komponenti .rtf formatidagi matn fayl yaratadi, shuning uchun quyidagicha yozgan ma’qul: RichEdit1->Lines->SaveToFile("a.rtf"); Barcha yuqoridagi komponentalar satr schyotchigiga ega: Memo1->Lines->Count; RichEdit1->Lines->Count; ListBox1->Items->Count; ComboBox1->Items->Count; Barcha komponentlar satrlarni o‘chirish, qo‘shish imkoniyatini beradi (ikkinchi satrni o‘chirish va o‘rniga yangi satr qo‘yish va ro‘yxat oxiriga yangi satr kiritishga misol): AnsiString S="Yes"; if (Memo1->Lines->Count >= 2) { Memo1->Lines->Delete(1); Memo1->Lines->Insert(1,S); } Memo1->Lines->Add(S); if (RichEdit1->Lines->Count >= 2) { RichEdit1->Lines->Delete(1); RichEdit1->Lines->Insert(1,S); } RichEdit1->Lines->Add(S); if(ListBox1->Items->Count >= 2) { ListBox1->Items->Delete(1); ListBox1->Items->Insert(1,S); } ListBox1->Items->Add(S); if(ComboBox1->Items->Count >= 2) { ComboBox1->Items->Delete(1); ComboBox1->Items->Insert(1,S); } ComboBox1->Items->Add(S); Satr ichidagilarni n satr nomeri orqali chiqarish mumkin: AnsiString S; S=Memo1->Lines->Strings[n]; S=RichEdit1->Lines->Strings[n]; S=ptStrintList->Strings[n]; S=ListBox1->Items->Strings[n]; S=ComboBox1->Items->Strings[n]; Satrga matnni o‘zlashtirib uning ustida amallar bajarish: AnsiString S; S=Memo1->Lines->GetText(); ... matn ustida amallar bajarish Memo1->Lines->SetText(S.c_str()); RichEdit1->Lines->LoadFromFile("a.txt"); S=RichEdit1->Lines->GetText(); ... matn ustida amallar bajarish RichEdit1->Lines->SetText(S.c_str()); S=ListBox1->Items->GetText(); ... matn ustida amallar bajarish ListBox1->Items->SetText(S.c_str()); S=ComboBox1->Items->GetText(); ... matn ustida amallar bajarish ComboBox1->Items->SetText(S.c_str()); TComboBox va TListBox komponentalari tanlangan satrlarga kirish imkonini beradi (OnChange xodisasida): void __fastcall TForm1::ComboBox1Change(TObject *Sender) { AnsiString S=ComboBox1->Items->Strings[ComboBox1->ItemIndex]; } void __fastcall TForm1::ListBox1Click(TObject *Sender) { AnsiString S=ListBox1->Items->Strings[ListBox1->ItemIndex]; } TComboBox va TListBox komponentalari satrdagi matnga mos satr nomerini aniqlash imkonini beradi: int n; n=ComboBox1->Items->IndexOf("Yes"); n=ListBox1->Items->IndexOf("Yes"); TComboBox va TlistBox satrlani saralash imkonini beradi: ListBox1->Sorted=true; ComboBox1->Sorted=true; Matnlar bilan ishlashda TfileStream klassi ko‘proq imkoniyatlarga ega. void __fastcall TForm1::Button1Click(TObject *Sender) { TFileStream * tfile=new TFileStream("b.txt",fmOpenReadWrite); tfile->Seek(0,0); char *vchrgBuf; if ((vchrgBuf = (char *) malloc(tfile->Size)) == NULL) { return; } tfile->Read(vchrgBuf,tfile->Size); AnsiString S=vchrgBuf; S.SetLength(tfile->Size); free(vchrgBuf); tfile->Size=0; Memo1->Lines->Clear(); Memo1->Lines->Add(S); if(Memo1->Lines->Count >= 2) { Memo1->Lines->Delete(1); String S1="WWWW"; Memo1->Lines->Insert(1,S1); } S=Memo1->Lines->GetText(); if ((vchrgBuf = (char *) malloc(S.Length())) == NULL) { return; } vchrgBuf=S.c_str(); tfile->Write(vchrgBuf,S.Length()); tfile->Free(); free(vchrgBuf); } Download 421.22 Kb. Do'stlaringiz bilan baham: |
Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling
ma'muriyatiga murojaat qiling