terça-feira, 27 de maio de 2014

Pesquisando String num Arquivo de Texto


A lógica principal deste código é fazera a leitura do arquivo
de texto como se fosse uma única string,
e isto se tornou possível, tirando o \n de cada linha do arquivo, 
bastou pra isto usar a função strlen ( string );que retorna o comprimento
da string, e estrategicamente usar -1, num arranjo entre colchetes.
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:





Veja abaixo o código do programa:

#include <stdio.h>

#include <conio2.h>


 //Salvar com a codificação ANSI

/*

Éder Costa

Humberto Gomes

Dijalma Lacerda

Caroline Silva

Igor Gonçalves

Bruna Carla

Fábio Quadros

Geany Barros

Ana Célia

Jaqueline Vega

*/


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 PESQUISANDO STRING NUM ARQUIVO DE TEXTO" );

          textcolor ( LIGHTRED );

          gotoxy ( 23, 3 );

          printf ( "PESQUISANDO STRING NUM ARQUIVO DE TEXTO" );

          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 ( void ) {

    FILE *arq;

    char **str;

    char str2 [ 100 ];

    char **str1;

    unsigned int i = 0;

    arq = fopen ( "Nomes.txt", "r" );

    str = ( char** ) malloc ( 20 * sizeof(char*) );

    str1 = ( char** ) malloc ( 20 * sizeof(char*) );

    continuando ( );

    system ( "cls" );

    Janela5 ( );

    textcolor ( LIGHTRED );

    gotoxy ( 23, 3 );

    printf ( "PESQUISANDO STRING NUM ARQUIVO DE TEXTO" );

    textcolor ( LIGHTBLUE );

    gotoxy ( 10, 5 );

    printf ( "Lendo abaixo o Arquivo" );

    if ( arq != NULL ) {

          while ( feof ( arq ) == 0 ) {

                 str [ i ] = ( char* ) malloc ( 20 * sizeof(char) );

                 fgets ( str [ 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 < 10; i++ ) {

          gotoxy ( 10, i + 7 );

          printf ( " %s ", str [ i ] );

    }

    getche ( );

    arq = fopen ( "Nomes.txt", "r" );

    if ( !arq )

          exit ( 1 );

    i = 0;

    textcolor ( LIGHTBLUE );

    gotoxy ( 40, 5 );

    printf ( "Digite uma string para pesquisar: " );

    str1 [ i ] = ( char* ) malloc ( 30 * sizeof(char*) );

    textcolor ( YELLOW );

    gotoxy ( 40, 7 );

    gets ( *str1 );

    str1 [ strlen ( *str1 ) - 1 ] = str1 [ strlen ( *str1 ) ];

    while ( fgets ( str2, sizeof ( str2 ), arq ) != NULL )

          if ( strstr ( str2, *str1 ) != NULL ) {

                 textcolor ( LIGHTGREEN );

                 gotoxy ( 40, 9 );

                 printf ( "A String " );

                 textcolor ( LIGHTRED );

                 gotoxy ( 49, 9 );

                 printf ( "%s ", *str1 );

                 textcolor ( LIGHTGREEN );

                 gotoxy ( 55, 9 );

                 printf ( "está no Arquivo " );

                 Sleep ( 1800 );

                 textcolor ( LIGHTRED );

                 gotoxy ( 45, 14 );

                 printf ( "MUITO OBRIGADO" );

                 getche ( );

                 return 0;

          }

    if ( strstr ( str2, *str1 ) == NULL ) {

          textcolor ( LIGHTGREEN );

          gotoxy ( 40, 9 );

          printf ( "A String " );

          textcolor ( LIGHTRED );

          gotoxy ( 49, 9 );

          printf ( "%s ", *str1 );

          textcolor ( LIGHTGREEN );

          gotoxy ( 55, 9 );

          printf ( "não está no Arquivo " );

          Sleep ( 1800 );

          textcolor ( LIGHTGRAY );

          gotoxy ( 50, 16 );

          printf ( "MUITO OBRIGADO" );

          getche ( );

          return 0;

    }

    fclose ( arq );

    return ( 0 );

}


Nenhum comentário:

Postar um comentário

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