quinta-feira, 28 de fevereiro de 2019

C++ builder criando e abrindo arquivo binário








//---------------------------------------------------------------------------
#include <fstream>
#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
bool x = false;
String str_1 = " ";
int a = 0, b = 0;
int A [ 10 ] [ 10 ], i;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1( TComponent* Owner )
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormPaint(TObject *Sender) {
 Canvas -> Pen -> Width = 10;
    Canvas -> Pen -> Color = clRed;
 Canvas -> Rectangle ( 05, 05, 595, 295 );
 Canvas -> Font -> Size = 14;
 Canvas -> Font -> Name = "arial";
 Canvas -> Font -> Color = clRed;
 Canvas -> TextOut ( 170, 10, "C++ BUILDER - ARQUIVO BINÁRIO" );
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click ( TObject *Sender ) {
fstream file;
 //Abre o arquivo para saída no modo binário.
 file.open ( "matriz.bin", ios::out | ios::binary );
 for( i = 0; i < 100; i++ ) {
  A [ b ] [ a ] =  i;
  a++;
 }
 //Escreve o conteúdo da matriz no arquivo.
 file.write ( reinterpret_cast < char * > ( A ), sizeof ( A ) );
 // Fecha o arquivo.
 file.close ( );
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click ( TObject *Sender ) {
 TFont *FT = new TFont;
 FT -> Name = "Algerian";
 FT -> Size = 14;
 FT -> Color = clBlack;
 FT -> Charset = ANSI_CHARSET;
 FT -> Style = TFontStyles ( ) << fsItalic;
 Label1 -> Font = FT;
 delete FT;
 fstream file;
 // Abre o arquivo para entrada no modo binário.
 file.open ( "matriz.bin", ios::in | ios::binary );
 //Ler o conteúdo do arquivo na matriz.
 file.read ( reinterpret_cast < char * > ( A ),
   sizeof ( A ) );
 for ( a = 0; a < 10; a++ ) {
  for ( b = 0; b < 10; b++ ) {
   str_1 += " ";
   if ( A [ a ] [ b ] % 10 == 0 ) {
    str_1 += ( "\n" );
   }
   if ( A [ a ] [ b ] >= 0 && A [ a ] [ b ] < 10 ) {
    str_1 += ( "0" );
   }
   str_1 += ( IntToStr ( A [ a ] [ b ] ) );
  }
 }
 if ( x == false ) {
 Label1 -> Caption = str_1;
  }
 //Fecha o arquivo.
 file.close ( );
 x = true;
}
//---------------------------------------------------------------------------


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

#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <System.Classes.hpp>
#include <Vcl.Controls.hpp>
#include <Vcl.StdCtrls.hpp>
#include <Vcl.Forms.hpp>
#include <Vcl.ExtCtrls.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
 TLabel *Label1;
 TButton *Button1;
 TButton *Button2;
 void __fastcall FormPaint ( TObject *Sender );
 void __fastcall Button1Click(TObject *Sender);
 void __fastcall Button2Click(TObject *Sender);
private: // User declarations
public:  // User declarations
 __fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif


object Form1: TForm1
  Left = 398
  Top = 192
  Caption = 'C++ BUILDER - ARQUIVO BIN'#193'RIO'
  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 = FormPaint
  PixelsPerInch = 96
  TextHeight = 13
  object Label1: TLabel
    Left = 182
    Top = 20
    Width = 5
    Height = 19
    Caption = ' '
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -16
    Font.Name = 'Tahoma'
    Font.Style = []
    ParentFont = False
  end
  object Button1: TButton
    Left = 182
    Top = 256
    Width = 75
    Height = 25
    Caption = 'Gerar arquivo'
    TabOrder = 0
    OnClick = Button1Click
  end
  object Button2: TButton
    Left = 376
    Top = 256
    Width = 75
    Height = 25
    Caption = 'Abrir arquivo'
    TabOrder = 1
    OnClick = Button2Click
  end

end