domingo, 10 de fevereiro de 2019

Lendo arquivo de texto - objeto Memo

Aqui está um exemplo de leitura de arquivo de texto,
utilizando o objeto Memo do C++ Builder..
O Memo possui diversas propriedades fundamentais,
e entre elas achei necessário ativar um Scroll vertical
e horizontal, além da font com o seu tamanho e cor
entre outras.
O arquivo tem 448 linhas, mas pode ser 
expandido para milhares de linhas.
A parte lógica do código foi feita em C, 
mas aceita adaptações para que se torne
totalmente em C++.







//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop
#define SI_ZE 450
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1 ( TComponent* Owner ) :
TForm ( Owner ) {
}
//---------------------------------------------------------------------------
void __fastcall TForm1::OnPaint ( TObject *Sender ) {
Canvas -> Font -> Size = 16;
Canvas -> Font -> Name = "Arial";
Canvas -> Pen -> Color = clRed;
Canvas -> Rectangle ( 05, 05, 590, 290 );
Canvas -> Pen -> Width = 10;
Canvas -> Font -> Color = clRed;
Canvas -> TextOut ( 60, 12, "C++ BUILDER - LENDO UM ARQUIVO DE TEXTO" );
String str_1 = " ";
FILE *arq;
char **no_me;
no_me = ( char** ) malloc ( SI_ZE * sizeof ( char* ) );
int c = 0, i;
arq = fopen ( "C:\\Users\\Embarcadero\\Documents\\Projetos C++ Builder\\OnPaint\\"
"Poesias famosas.txt", "r+b" );
if ( arq == NULL ) {
printf ( "Problemas na abertura do arquivo " );
}
for ( i = 0; i < SI_ZE; i++ ) {
no_me [ i ] = ( char* ) malloc ( SI_ZE * sizeof ( char* ) / 3 );
fgets ( no_me [ i ], 80, arq );
str_1 += i;
str_1 += "  ";
str_1 += no_me [ i ];
}
Memo1 -> Text = Memo1 -> Text + AnsiString ( str_1 );
Memo1 -> SelStart = 0;
free ( no_me );
fclose ( arq );
}

//---------------------------------------------------------------------------


//---------------------------------------------------------------------------

#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <System.Classes.hpp>
#include <Vcl.Controls.hpp>
#include <Vcl.StdCtrls.hpp>
#include <Vcl.Forms.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
TMemo *Memo1;
void __fastcall OnPaint(TObject *Sender);
private: // User declarations
public: // User declarations
__fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif


object Form1: TForm1
  Left = 484
  Top = 207
  Caption = 'C++ BUILDER - LENDO UM ARQUIVO DE TEXTO'
  ClientHeight = 300
  ClientWidth = 600
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  Position = poDesigned
  OnPaint = OnPaint
  PixelsPerInch = 96
  TextHeight = 13
  object Memo1: TMemo
    Left = 40
    Top = 48
    Width = 521
    Height = 225
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -19
    Font.Name = 'Tahoma'
    Font.Style = [fsItalic]
    ParentFont = False
    ScrollBars = ssBoth
    TabOrder = 0
  end
end



Nenhum comentário:

Postar um comentário

Observação: somente um membro deste blog pode postar um comentário.