segunda-feira, 19 de outubro de 2015

Arquivo - escolhendo colunas para leitura

Programadores em linguagem c tem que ter pleno domínio em manipulação
de arquivos, e para provar que não estamos de blá-blá-blá, criei
este programa interessante.
No programa são usados duas funções para leitura nos arquivos,que é a
função fscanf(); e a função fgets();
Más o impressionante aqui é que temos a ousadia de escolher as colunas
do arquivo a ser lida, ou o arquivo inteiro é claro.
Tá certo que utilizei a função fseek(); em conjunto com a
função fscanf();se não seria quase impossível conseguir isto.
Já ja função fggets(); que é muito mais fácil de trabalhar não se fez necessário a função fseek(); para posicionar a leitura só da coluna dos nomes.
Fica difícil falar aqui de cada uma das funções, comandos, ou linha de códigos utilizados no programa, más disponibilizo gratuitamente
o código para todos os amantes de linguagem c.

Veja abaixo imagens do programa em execução:










Veja abaixo o código do programa:



#include <conio.h>
#include <stdio.h>
int main ( );
void Janela ( ) {
     int lin, col;
     for ( lin = 1; lin <= 26; lin++ ) {
         for ( col = 1; col <= 80; col++ ) {
              gotoxy ( col, lin );
              if ( lin == 2 ) {
                   textattr ( 200 );
                   printf ( " " );
              }
              if ( col == 1 ) {
                   textattr ( 200 );
                   printf ( " " );
              }
              if ( col == 80 ) {
                   textattr ( 200 );
                   printf ( " " );
              }
         }
     }
}
void Moldura ( int a, int c, int b, int d, int e, int f ) {
     textbackground ( WHITE );
     for ( a = e; a <= c; a++ )
         for ( b = f; b <= d; b++ ) {
              gotoxy ( b, a );
              textbackground ( WHITE );
              printf ( " " );
         }
}
int Le_Arquivo_Floats ( int a, int b ) {
     system ( "cls" );
     Janela ( );
     textbackground ( WHITE );
     textcolor ( LIGHTRED );
     gotoxy ( 28, 3 );
     printf ( "ARQUIVO - ESCOLHENDO COLUNAS" );
     float nome [ ] [ 3 ] = { 0 };
     int i, j;
     int l;
     FILE *arq;
     if ( ( arq = fopen ( "C:\\Users\\SAMUEL 64SP1\\Desktop\\Arquivos\\"
              "Concurso Belas da informática.txt", "r" ) ) == NULL ) {
         printf ( "Houve um erro na abertura do arquivo" );
         getche ( );
         exit ( 1 );
     }
     l = 0;
     textcolor ( BLACK );
     for ( l = 0; l < 18; l++ ) {
         fseek ( arq, 21, SEEK_CUR );
         for ( i = 0; i < 18; i++ ) {
              for ( j = 21; j < 33; j++ ) {
                   fscanf ( arq, "%f", &nome [ i ] [ j ] );
              }
         }
         i = 0;
         gotoxy ( 33, l - 3 );
         for ( j = b; j < a; j++ ) {
              if ( ( l > 7 ) || ( l < 0 ) ) {
                   printf ( " %.2f ", nome [ i ] [ j ] );
              }
         }
     }
     textcolor ( LIGHTRED );
     gotoxy ( 31, 23 );
     printf ( "PRESSIONE QUALQUER TECLA" );
     getche ( );
     textbackground ( LIGHTRED );
     return main ( );
}
int Le_Aquivo_Inteiro ( int a, int b ) {
     system ( "cls" );
     textcolor ( LIGHTRED );
     gotoxy ( 28, 4 );
     printf ( "ARQUIVO - ESCOLHENDO COLUNAS" );
     char nome [ 18 ] [ 51 ] = { 0 };
     int i, j, l;
     FILE *arq;
     if ( ( arq = fopen ( "C:\\Users\\SAMUEL 64SP1\\Desktop\\Arquivos\\"
              "Concurso Belas da informática.txt", "r" ) ) == NULL ) {
         printf ( "Houve um erro na abertura do arquivo" );
         getche ( );
         exit ( 1 );
     }
     if ( arq == NULL ) {
         printf ( "\nArquivo nao encontrado" );
         getche ( );
         return 0;
     }
     l = 10;
     textcolor ( BLACK );
     gotoxy ( 1, 6 );
     for ( i = 0; i < 17; i++ ) {
         fseek ( arq, 0, SEEK_CUR );
         for ( j = 1; j < 51; j++ ) {
              fscanf ( arq, "%c", &nome [ i ] [ j ] );
              printf ( "%c", nome [ i ] [ j ] );
         }
     }
     Janela ( );
     fclose ( arq );
     textbackground ( WHITE );
     textcolor ( LIGHTRED );
     gotoxy ( 31, 23 );
     printf ( "PRESSIONE QUALQUER TECLA" );
     getche ( );
     textbackground ( LIGHTRED );
     return main ( );
}
int main ( ) {
     system ( "title ARQUIVO - ESCOLHENDO COLUNAS" );
     int a, b = 0;
     char nome [ 17 ] [ 33 ];
     int i, j;
     FILE *arq;
     if ( ( arq = fopen ( "C:\\Users\\SAMUEL 64SP1\\Desktop\\Arquivos\\"
              "Concurso Belas da informática.txt", "r" ) ) == NULL ) {
         printf ( "Houve um erro na abertura do arquivo" );
         getche ( );
         exit ( 1 );
     }
     do {
         system ( "cls" );
         textbackground ( LIGHTRED );
         Moldura ( 2, 24, 2, 78, 2, 3 );
         textcolor ( LIGHTRED );
         gotoxy ( 28, 3 );
         printf ( "ARQUIVO - ESCOLHENDO COLUNAS" );
         textcolor ( LIGHTBLUE );
         gotoxy ( 22, 5 );
         printf ( "O arquivo tem 4 colunas faça uma escolha" );
         textcolor ( BLACK );
         gotoxy ( 15, 7 );
         printf ( "1 - Escolher le o arquivo inteiro " );
         gotoxy ( 15, 8 );
         printf ( "2 - Escolher le só a coluna dos nomes das candidatas" );
         gotoxy ( 15, 9 );
         printf ( "3 - Escolher le só a coluna das alturas das candidatas" );
         gotoxy ( 15, 10 );
         printf ( "4 - Escolher le só a coluna dos pesos das candidatas" );
         gotoxy ( 15, 11 );
         printf ( "5 - Escolher le só a coluna das idades das candidatas" );
         gotoxy ( 15, 12 );
         printf ( "6 - Escolher le as colunas altura, peso e idades" );
         gotoxy ( 15, 13 );
         printf ( "7 - Finalizar o programa" );
         gotoxy ( 15, 15 );
         scanf ( "%d", &a );
         fflush ( stdin );
         gotoxy ( 15, 15 );
         clreol ( );
         if ( a == 1 ) {
              a = 51;
              b = 1;
              Le_Aquivo_Inteiro ( a, b );
         }
         if ( a == 2 ) {
              system ( "cls" );
              textcolor ( LIGHTRED );
              gotoxy ( 28, 4 );
              printf ( "ARQUIVO - ESCOLHENDO COLUNAS" );
              textbackground ( WHITE );
              textcolor ( BLACK );
              for ( i = 0; i < 18; i++ ) {
                   fgets ( nome [ i ], 160, arq );
              }
              for ( i = 5; i < 16; i++ ) {
                   gotoxy ( 35, i + 2 );
                   for ( j = 16; j < 31; j++ ) {
                        if ( ( i > 5 ) || ( i < 0 ) ) {
                            printf ( "%c", nome [ i ] [ j ] );
                        }
                   }
              }
              Janela ( );
              fclose ( arq );
              textbackground ( WHITE );
              textcolor ( LIGHTRED );
              gotoxy ( 31, 23 );
              printf ( "PRESSIONE QUALQUER TECLA" );
              getche ( );
              textbackground ( LIGHTRED );
              return main ( );
              break;
         }
         if ( a == 3 ) {
              a = 22;
              b = 21;
              Le_Arquivo_Floats ( a, b );
         }
         if ( a == 4 ) {
              a = 23;
              b = 22;
              Le_Arquivo_Floats ( a, b );
         }
         if ( a == 5 ) {
              a = 24;
              b = 23;
              Le_Arquivo_Floats ( a, b );
         }
         if ( a == 6 ) {
              a = 24;
              b = 21;
              Le_Arquivo_Floats ( a, b );
         }
         if ( a == 7 ) {
              textcolor ( LIGHTBLUE );
              gotoxy ( 17, 17 );
              printf ( "Por: " );
              textcolor ( LIGHTMAGENTA );
              printf ( "Samuel Lima" );
              textcolor ( BLACK );
              gotoxy ( 17, 19 );
              printf ( "sa_sp10@hotmail.com" );
              Sleep ( 1800 );
              textcolor ( LIGHTRED );
              gotoxy ( 36, 23 );
              printf ( "MUITO OBRIGADO" );
              getch ( );
              exit ( 0 );
         }
         if ( a < 1 || a > 7 ) {
              textcolor ( LIGHTRED );
              gotoxy ( 37, 17 );
              printf ( "\aOpção errada" );
              getche ( );
         }
     } while ( ( a < 1 ) || ( a > 7 ) );
     return 0;
}



Nenhum comentário:

Postar um comentário

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