terça-feira, 3 de junho de 2014

Pesquisando String em Arquivo II


A leitura do arquivo contendo os nomes cadastrados é feita normalmente, com uso padrão para este tipo de situação, e não cabe a mim ensinar como isto é feito,porém são copiados numa Matriz do tipo char e são imprimidos por printf();
Quando acontece a solicitação de uma entrada, são procurados pela função strstr(); dentro da Matriz de caracteres do tipo char, quando encontrado é mostrado o nome e a sua linha onde se encontra.
Vale lembrar que a função strstr ( string1, string2 ); também da biblioteca strin.h, permite localizar uma substring dentro de uma string,
e seu uso é muito fácil, como todas as outras funções desta biblioteca, que estão prontas para o uso, com poucas modificações.

Veja imagens do programa em execução:






Estes Nomes abaixo foram usados como testes:




/*
 Éder Costa 
 Caroline Silva
 Humberto Gomes
 Dijalma Lacerda
 Igor Gonçalves
 Bruna Carla 
 Fábio Quadros
 Geany Barros
 Jaqueline Vega
 Ana Célia

 Salve - os num arquivo de texto com o nome de Nomes.txt,
 e coloque do lado do .exe do Programa.
 */

Veja abaixo o código do programa:


#include <stdio.h>
#include <conio.h>
#define NAOENCONTROU 0
void Janela5 ( ) {
     int lin, col;
     col = 0;
     system ( "color A0" );
     for ( lin = 0; lin <= 25 ; lin++ ) {
         for ( col = 0; col <= 80 ; col++ ) {
              gotoxy ( col , lin );
              if ( lin == 2 ) {
                   textbackground ( BLACK );
                   printf ( " " );
              }
              if ( col == 1 ) {
                   textbackground ( BLACK );
                   printf ( " " );
              }
              if ( lin == 25 ) {
                   textbackground ( BLACK );
              }
              if ( col == 80 ) {
                   textbackground ( BLACK );
                   printf ( " " );
              }
         }
     }
     textbackground ( BLACK );
}
int continuando ( ) {
     int nr;
     do {
         system ( "cls" );
         Janela5 ( );
         textcolor ( LIGHTRED );
         gotoxy ( 26 , 3 );
         printf ( "PESQUISANDO STRING EM ARQUIVO II" );
         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 ( 24 , 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 ( 33 , 18 );
              printf ( "O ROCCO AGRADECE!" );
              getche ( );
              exit ( 0 );
         } else if ( nr == 1 ) {
              return 1;
         }
         textcolor ( YELLOW );
         gotoxy ( 26 , 16 );
         printf ( "\aopcao errada!" );
         getche ( );
     } while ( 1 );
     return 1;
}
int main ( void ) {
     system ( "title PESQUISANDO STRING EM ARQUIVO II" );
     continuando ( );
     system ( "cls" );
     Janela5 ( );
     FILE *arq;
     int i, item = 0, tent;
     const int TAM = 17;
     char nome [ 10 ] [ TAM ];
     char buffer [ 10 ] [ TAM ];
     char *resul;
     char no_me [ 15 ];
     arq = fopen ( "Nomes.txt" , "r" );
     textcolor ( LIGHTRED );
     gotoxy ( 25 , 3 );
     printf ( "PESQUISANDO STRING EM ARQUIVO II " );
     textcolor ( LIGHTBLUE );
     gotoxy ( 6 , 5 );
     printf ( "ARQUIVO TXT" );
     if ( arq != NULL ) {
         textcolor ( WHITE );
         for ( i = 0; i < 10 ; i++ ) {
              gotoxy ( 5 , i + 7 );
              resul = fgets ( *buffer , TAM , arq );
              strcpy ( nome [ i ] , *buffer );
              printf ( " %s " , *buffer );
         }
         fclose ( arq );
         Sleep ( 1000 );
     } else {
         printf ( "Nao foi possível abrir o arquivo." );
         getche ( );
     }
     textcolor ( LIGHTBLUE );
     gotoxy ( 46 , 5 );
     printf ( "CÓPIA DO ARQUIVO " );
     textcolor ( WHITE );
     for ( i = 0; i < 10 ; i++ ) {
         gotoxy ( 45 , i + 7 );
         printf ( " %s " , nome [ i ] );
     }
     Sleep ( 1000 );
     textcolor ( LIGHTRED );
     gotoxy ( 32 , 22 );
     printf ( "PRESSIONE QUALQUER TECLA" );
     getche ( );
     do {
         system ( "cls" );
         Janela5 ( );
         textcolor ( LIGHTRED );
         gotoxy ( 25 , 3 );
         printf ( "PESQUISANDO STRING EM ARQUIVO II" );
         textcolor ( LIGHTBLUE );
         gotoxy ( 15 , 5 );
         printf ( "Digite um nome para procurar no Arquivo: " );
         textcolor ( YELLOW );
         gets ( no_me );
         fflush ( stdin );
         for ( i = 0; i < 10 ; i++ ) {
              if ( strstr ( nome [ i ] , no_me ) ) {
                   gotoxy ( 5 , 7 );
                   textcolor ( LIGHTBLUE );
                   printf ( " O Nome " );
                   textcolor ( YELLOW );
                   printf ( "%s" , no_me );
                   textcolor ( LIGHTBLUE );
                   printf ( " está na  linha " );
                   textcolor ( LIGHTRED );
                   printf ( "%d" , i );
                   textcolor ( LIGHTBLUE );
                   printf ( " do Arquivo" );
                   textcolor ( DARKGRAY );
                   printf ( "\n\n" );
                   for ( i = 0; i < 10 ; i++ ) {
                        gotoxy ( 5 , i + 9 );
                        textcolor ( LIGHTBLUE );
                        printf ( " Linha " );
                        textcolor ( LIGHTRED );
                        printf ( " %d " , i );
                        textcolor ( LIGHTBLUE );
                        printf ( " :" );
                        textcolor ( YELLOW );
                        printf ( " %s " , nome [ i ] );
                   }
                   Sleep ( 1800 );
                   textcolor ( LIGHTRED );
                   gotoxy ( 36 , 23 );
                   printf ( "MUITO OBRIGADO" );
                   getche ( );
                   return 0;
              }
         }
         if ( item == NAOENCONTROU ) {
              do {
                   textcolor ( YELLOW );
                   gotoxy ( 25 , 7 );
                   printf ( "Este Nome não foi achado no Arquivo" );
                   textcolor ( LIGHTBLUE );
                   gotoxy ( 10 , 9 );
                   printf (
                            "digite [ 1 ] Para Tentar novamente ou [ 2 ] para sair          " );
                   textcolor ( LIGHTMAGENTA );
                   gotoxy ( 64 , 9 );
                   scanf ( "%d" , &tent );
                   fflush ( stdin );
                   if ( tent == 1 ) {
                        break;
                   }
                   if ( tent == 2 ) {
                        textcolor ( LIGHTCYAN );
                        gotoxy ( 36 , 23 );
                        printf ( "MUITO OBRIGADO" );
                        Sleep ( 1800 );
                        exit ( 0 );
                   } else {
                        textcolor ( LIGHTRED );
                        gotoxy ( 35 , 13 );
                        printf ( "\aOpção errada" );
                        getche ( );
                        gotoxy ( 33 , 13 );
                        printf ( "                              " );
                   }
              } while ( 1 );
         }
     } while ( 1 );
     return ( 0 );
}


Nenhum comentário:

Postar um comentário

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