domingo, 21 de abril de 2019

Arquivo - escolhendo linha para leitura

Aqui está um exemplo de como ler
uma linha específica dentro de um arquivo,
o código é didático e indicado a estudantes
de linguagem C.


Veja abaixo o código do programa:


#include <stdio.h>
#include <conio.h>

#define ENTER 13
#define tam 10
bool x = false;
char letra = '\0';
int a = 0, i = 0, m = -1;
/*============================================================================*/
//Criando uma moldura na tela do dos
int Moldura ( int tam_lin_ini, int tam_lin_fim,
         int tam_ini_col, int tam_fim_col ) {
    int i, c;
    for ( i = tam_lin_ini; i < tam_lin_fim; i++ ) {
         for ( c = tam_ini_col; c < tam_fim_col; c++ ) {
             gotoxy ( c, i );
             textbackground ( WHITE );
             printf ( " " );
         }
    }
    return 0;
}
/*============================================================================*/
void Informe ( ) {
    if ( x == false ) {
         gotoxy ( 38, 23 );
         textcolor ( LIGHTRED );
         printf ( "TECLE ENTER" );
    } if ( x == true )  {
         textcolor ( LIGHTBLUE );
         gotoxy ( 26, 20 );
         printf ( "Criado por: " );
         textcolor ( LIGHTMAGENTA );
         gotoxy ( 126, 20 );
         printf ( "Samuel Lima" );
         textcolor ( BLACK );
         gotoxy ( 23, 23 );
         printf ( "                                " );
         gotoxy ( 26, 21 );
         printf ( "sa_sp10@hotmail.com" );
         textcolor ( LIGHTRED );
         gotoxy ( 36 , 24 );
         printf ( "MUITO OBRIGADO" );
    }
}
/*============================================================================*/
void Tecle_Enter ( ) {
    //Este bloco só permite a passagem pela tecla ENTER
    //Se alum caractere for digitado, um beep é acionado
    do {
         Informe ( );
         letra = getch ( );
         fflush ( stdin );
         if ( letra != ENTER ) {
             Beep ( 1000, 500 );
         }
         continue;
    }while ( letra != ENTER );
}
/*============================================================================*/
void Insere_Valor ( ) {
    Tecle_Enter ( );
    do {
         Informe ( );
         textcolor ( LIGHTBLUE );
         gotoxy ( 30 , 5 );
         printf ( "Digite um número de linha: " );
         textcolor ( LIGHTRED );
         scanf ( "%d", &m );
         fflush ( stdin );
         if ( ( m < 1 ) || ( m > tam ) ) {
             Beep ( 1000, 500 );
             gotoxy ( 57 , 5 );
             clreol ( );
         }
    } while ( ( m < 1 ) || ( m > tam ) );
}
/*============================================================================*/
void Ler_Arquivo ( char **nomes ) {
    do {
         a++;
         FILE *arq;
         if ( ( arq = fopen ( "Nomes.txt" , "rb" ) ) == NULL ) {
             printf ( "Houve um erro na abertura do arquivo" );
             getche ( );
             exit ( 1 );
         }
         while ( feof ( arq ) == 0 ) {
             nomes [ i ] = ( char* ) malloc ( 80 * sizeof(char) );
             fgets ( nomes [ i ] , 80 , arq );
             ++i;
         }
         textcolor ( BLACK );
         for ( i = 0; i < tam ; i++ ) {
             gotoxy ( 5 , i + 7 );
             printf ( "%s" , nomes [ i ] );
         }
         Insere_Valor ( );
         while ( feof ( arq ) == 0 ) {
             nomes [ i ] = ( char* ) malloc ( 80 * sizeof ( char ) );
             fgets ( nomes [ i ] , 80 , arq );
             ++i;
         }
         gotoxy ( 35 , m + 6 );
         printf ( "%s" , nomes [ m - 1 ] );
         Sleep ( 1000 );
         gotoxy ( 57 , 5 );
         clreol ( );
         fclose ( arq );
         if ( a == tam )
             x = true;
    }while ( 1 );
}
/*============================================================================*/
int main ( ) {
    system ( "title LENDO UMA LINHA ESPECÍFICA NUM ARQUIVO" );
    char **nomes;
    nomes = ( char** ) malloc ( 80 * sizeof ( char* ) );
    Moldura ( 2, 25, 3, 79 );
    textcolor ( LIGHTRED );
    gotoxy ( 23 , 3 );
    printf ( "LENDO UMA LINHA ESPECÍFICA NUM ARQUIVO" );
    textcolor ( LIGHTBLUE );
    gotoxy ( 7 , 5 );
    printf ( "Nomes do arquivo" );
    Ler_Arquivo ( nomes );
    free ( nomes );
    return ( 0 );
}