domingo, 14 de setembro de 2014

Fwrite e Fread - struct em Arquivo Bin

As structs Notas_Idades e Alu_no, são gravadas normalmente num arquivo binário,
porém na hora da leitura por fread(); são copiadas em outras estruturas, respectivamente
struct Co_pia e struct Copia_foat, garantindo um bom funcionamento do programa e não
deixando dúvidas sobre a leitura do arquivo, e logo o arquivo copiado nas estruturas é
imprimido por printf(); que dentro de um laço for é varrido.
Teste este código e aproveite seu excelente funcionamento.

Veja duas imagens do programa em execução:



Veja abaixo o código do programa:

#include <stdio.h>
#include <conio.h>
void Janela5(){
    int lin, col;system("color 6F");
    for ( lin = 0; lin <= 36; lin++ ){
        for ( col = 0; col <= 80; col++ ){
            gotoxy( col, lin );
            if ( lin == 2 ){textbackground(BLACK);printf( " ");}
            if ( col == 1 ){textbackground(BLACK);printf(" ");}
            if ( lin == 36 ){textbackground(BLACK);}
            if ( col == 80 ){textbackground(BLACK);printf(" ");}
        }
    }textbackground( BLACK );
}
void inicio( ){    system ("title FWRITE E FREAD - STRUCT EM ARQUIVO BIN");
int i;system("cls");do{Janela5();
textcolor(LIGHTRED);gotoxy(25,7);printf("FWRITE E FREAD - STRUCT EM ARQUIVO BIN");
textcolor(YELLOW);gotoxy(25,10);printf("Programa desenvolvido por:");
textcolor(LIGHTCYAN);gotoxy(52,10);printf("Samuel Lima");
textcolor(LIGHTGREEN);gotoxy(34,12);printf("sa_sp10@hotmail.com");
textcolor(LIGHTBLUE);gotoxy(24,14);printf("DIGITE    PARA SAIR OU   PARA CONTINUAR  ");
textcolor(LIGHTRED);gotoxy(32,14);printf("1");textcolor(LIGHTRED);gotoxy(47,14);printf("2");
gotoxy(41,16);
scanf("%d",&i);fflush(stdin);
if( i == 1 ){textcolor(LIGHTRED);gotoxy(35,20);printf ("MUITO OBRIGADO");Sleep(1800);exit(0);}
if( i == 2 ){system("cls"); return;}
else {textcolor(LIGHTRED);gotoxy(37,20);printf ("\aOPÇÃO ERRADA");Sleep(1800);system("cls");}
}
while(i);
}
struct Classe{
    float Idade;
    float Notas;
    int Chamada;
};
struct Classe Notas_Idades [ 10 ] =  { { 20.4, 9.5, 1}, {18.6, 8.9, 5}, {16.5, 6.4, 8 },
        { 19.4, 7.5, 4}, {17.5, 8.0, 3}, {17.0, 7.7, 9 },
        { 19.7, 6.9, 2}, {18.6, 9.0, 6}, {16.0, 10.0, 7 },
        {20.0, 8.5, 10 }};
struct Copia_float {
    float Ida_de;
    float Not_as;
    int Cham_ada;
};
struct Copia_float Copia_float [ 10 ];
struct Escola {
    char nome [ 16 ];
};
struct Escola Alu_no [ 16 ] = {
        { "Ana Célia      "  },
        { "Éder Costa     "  },
        { "Humberto Gomes "  },
        { "Dijalma Lacerda"  },
        { "Caroline Silva "  },
        { "Igor Goncalves "  },
        { "Bruna Carla    "  },
        { "Fábio Quadros  "  },
        { "Geany Barros   "  },
        { "Jaqueline Vega "  }};
struct Copia {
    char No_me [ 16 ];
};
struct Copia Co_pia [ 16 ];
void Cria_Arquivo_Struct(){
    FILE *arq;
    if ( ( arq = fopen ("Arquivo.bin", "wb" ) ) == NULL ){
        printf("Houve um erro na criação do arquivo");
        getche();exit(1);
    }
    if ( fwrite ( &Alu_no, sizeof ( Alu_no ), 1, arq ) != 1 ){
        printf("Houve um erro na gravação do arquivo");
        getche();exit(1);
    }
    if ( fwrite ( &Notas_Idades, sizeof ( Notas_Idades ), 1, arq ) != 1 ){
        printf("Houve um erro na gravação do arquivo");
        getche();exit(1);
    }
    fclose(arq);
}
void Abre_Arquivo_Struct(){
    FILE *arq;int i;
    if ( ( arq = fopen("Arquivo.bin", "rb" ) ) == NULL ){
        printf("Houve um erro na abertura do arquivo");
        getche();exit(1);
    }
    if ( fread ( &Co_pia, sizeof ( Co_pia ), 1, arq ) != 1 ){
        printf("Houve um erro na leitura do arquivo");
        getche();exit(1);
    }
    if ( fread ( &Copia_float, sizeof ( Copia_float ), 1, arq ) != 1 ){
        printf("\a Houve um erro na leitura do arquivo");
        getche();exit(1);
    }
    textcolor(LIGHTRED);gotoxy(24,3);printf("FWRITE E FREAD - STRUCT EM ARQUIVO BIN");
    textcolor(YELLOW);gotoxy(11,5);printf("Nomes                Idades          Notas         Números");
    printf("\n\n");textcolor(LIGHTBLUE);
    for( i = 0; i < 10; i++ ){
        printf("\t%s  \t%.1f  \t\t%.1f  \t\t%d\n", Co_pia [ i ] . No_me,
                Copia_float [ i ] . Ida_de, Copia_float [ i ] . Not_as,
                Copia_float [ i ] . Cham_ada );
    }
    fclose(arq);Sleep(1800);
    textcolor(LIGHTRED);gotoxy(35,20);printf ("MUITO OBRIGADO");
    getche();exit(0);
}
int main(){
    inicio( );
    Cria_Arquivo_Struct();
    Abre_Arquivo_Struct();
}

Nenhum comentário:

Postar um comentário

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