domingo, 25 de maio de 2014

Preenchendo os campos de uma struct com Arquivo


Preencher os campos de uma struct usando dados de um arquivo
de texto não é tão difícil como parece, e neste código mostro
uma das maneiras de fazer isto.
usei um cadastro simples só pra testes, más pode ser amplamente modificado, por quem entende de programação em c.

Veja abaixo o único e simples cadastro usado:

Éder Costa  
Rua das Primícias
579

Pra testar, salve com o nome de: Nomes.txt, e coloque do lado do
programa compilado.

Veja abaixo algumas imagens do programa em execução:




Veja o código do programa abaixo:

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
struct Cadastro {
     char Nome [ 20 ];
     char Endereco [ 20 ];
     char num [ 5 ];
} Cada_stro;
void Janela5 ( ) {
     int lin, col;
     for ( lin = 0; lin <= 25; lin++ ) {
         for ( col = 0; col <= 80; col++ ) {
              gotoxy ( col, lin );
              if ( lin == 2 ) {
                   textbackground ( LIGHTBLUE );
                   printf ( " " );
              }
              if ( col == 1 ) {
                   textbackground ( LIGHTBLUE );
                   printf ( " " );
              }
              if ( lin == 25 ) {
                   textbackground ( LIGHTBLUE );
              }
              if ( col == 80 ) {
                   textbackground ( LIGHTBLUE );
                   printf ( " " );
              }
         }
     }
     textbackground ( BLACK );
}
int continuando ( ) {
     int nr;
     do {
         system ( "cls" );
         Janela5 ( );
         system ( "title PREENCHENDO OS CAMPOS DE UMA STRUCT COM ARQUIVO" );
         textcolor ( LIGHTRED );
         gotoxy ( 19, 3 );
         printf ( "PREENCHENDO OS CAMPOS DE UMA STRUCT COM ARQUIVO" );
         textcolor ( YELLOW );
         gotoxy ( 24, 5 );
         printf ( "Programa desenvolvido por:" );
         textcolor ( LIGHTGRAY );
         gotoxy ( 51, 5 );
         printf ( "Samuel Lima" );
         textcolor ( LIGHTGREEN );
         gotoxy ( 34, 7 );
         printf ( "sa_sp10@hotmail.com" );
         textcolor ( LIGHTBLUE );
         gotoxy ( 23, 9 );
         printf ( "Digite 0 para sair ou 1 para continuar : " );
         textcolor ( LIGHTRED );
         scanf ( "%d", &nr );
         fflush ( stdin );
         fflush ( stdin );
         if ( nr == 0 ) {
              textcolor ( LIGHTRED );
              gotoxy ( 35, 21 );
              printf ( "MUITO OBRIGADO" );
              getche ( );
              exit ( 0 );
         } else if ( nr == 1 ) {
              return 1;
         }
         textcolor ( YELLOW );
         gotoxy ( 36, 16 );
         printf ( "\aopção errada!" );
         getche ( );
     } while ( 1 );
     return 1;
}
int main ( ) {
     FILE *arq;
     unsigned int i = 0;
     char **nomes;
     arq = fopen ( "Nomes.txt", "r" );
     nomes = ( char** ) malloc ( 20 * sizeof(char*) );
     continuando ( );
     system ( "cls" );
     Janela5 ( );
     textcolor ( LIGHTRED );
     gotoxy ( 18, 3 );
     printf ( "PREENCHENDO OS CAMPOS DE UMA STRUCT COM ARQUIVO" );
     textcolor ( LIGHTBLUE );
     gotoxy ( 20, 5 );
     printf ( "Lendo Primeiro o Arquivo com o Cadastro" );
     if ( arq != NULL ) {
         while ( feof ( arq ) == 0 ) {
              nomes [ i ] = ( char* ) malloc ( 20 * sizeof(char) );
              fgets ( nomes [ i ], 20, arq );
              ++i;
         }
         fclose ( arq );
     } else {
         textcolor ( YELLOW );
         gotoxy ( 24, 12 );
         printf ( "Nao foi possível abrir o arquivo." );
         getche ( );
         exit ( 0 );
     }
     textcolor ( LIGHTRED );
     for ( i = 0; i < strlen ( nomes [ i ] ); i++ ) {
         gotoxy ( 30, i + 7 );
         printf ( " %s ", nomes [ i ] );
         strcpy ( Cada_stro.Nome, nomes [ 0 ] );
         strcpy ( Cada_stro.Endereco, nomes [ 1 ] );
         strcpy ( Cada_stro.num, nomes [ 2 ] );
     }
     Sleep ( 800 );
     i = strlen ( Cada_stro.Nome );
     int j = strlen ( Cada_stro.Endereco );
     int l = strlen ( Cada_stro.num );
     if ( i > 0 && Cada_stro.Nome [ i - 1 ] )
         Cada_stro.Nome [ i - 1 ] = '\0';
     textcolor ( LIGHTBLUE );
     gotoxy ( 25, 11 );
     printf ( "Lendo abaixo o Cadastro na struct" );
     textcolor ( YELLOW );
     gotoxy ( 30, 13 );
     printf ( "Nome: %s", Cada_stro.Nome );
     Sleep ( 800 );
     gotoxy ( 30, 14 );
     if ( j > 0 && Cada_stro.Endereco [ j - 1 ] )
         Cada_stro.Endereco [ j - 1 ] = '\0';
     printf ( "Rua: %s", Cada_stro.Endereco );
     Sleep ( 800 );
     gotoxy ( 30, 15 );
     if ( l > 0 && Cada_stro.num [ l - 1 ] )
         Cada_stro.num [ l - 1 ] = '\0';
     printf ( "Número: %s", Cada_stro.num );
     Sleep ( 800 );
     textcolor ( LIGHTRED );
     gotoxy ( 35, 21 );
     printf ( "MUITO OBRIGADO" );
     getche ( );
}


Nenhum comentário:

Postar um comentário

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