sábado, 17 de janeiro de 2015

Arquivo - extraindo linhas para escrita

Talvez você tentou editar num arquivo escolhendo
o local exato onde escrever e teve dificuldades
e acabou deixando pra lá.
Em programação você não pode dexistir de nada
não importa o tempo que leve pra se fazer, más faça
porque é assim que se aprende.
Neste código mostro um dos modos de escrever num
arquivo escolhendo a linha de início e de fim,
mantendo o restante das linhas originais.

Usei como texto no arquivo, a primeira parte do
Hino à Bandeira Nacional.

Veja abaixo imagens do programa em execução:




Veja abaixo o código do programa:

#include <stdio.h>
#include <conio.h>
#define EDITA '\0'
#define SI_ZE 16
#define re_al  real
/*
 Salve, lindo pendão da esperança,
 Salve, símbolo augusto da paz!
 Tua nobre presença à lembrança
 A grandeza da Pátria nos traz.

 Recebe o afeto que se encerra
 Em nosso peito juvenil,
 Querido símbolo da terra,
 Da amada terra do Brasil!

 Em teu seio formoso retratas
 Este céu de puríssimo azul,
 A verdura sem par destas matas,
 E o esplendor do Cruzeiro do Sul.

 Recebe o afeto que se encerra
 Em nosso peito juvenil,
 Querido símbolo da terra,
 Da amada terra do Brasil!
 */
int main ( void );
void Janela5 ( ) {
     int l, c;
     system ( "Color F0" );
     for ( l = 0; l <= 25; l++ ) {
         for ( c = 0; c <= 80; c++ ) {
              gotoxy ( c, l );
              if ( l == 2 ) {
                   textbackground ( LIGHTGRAY );
                   printf ( " " );
              }
              if ( c == 1 ) {
                   textbackground ( LIGHTGRAY );
                   printf ( " " );
              }
              if ( l == 25 ) {
                   textbackground ( LIGHTGRAY );
              }
              if ( c == 80 ) {
                   textbackground ( LIGHTGRAY );
                   printf ( " " );
              }
         }
     }
     textbackground ( LIGHTGRAY );
}
int continuando ( ) {
     system ( "title ARQUIVO - EXTRAINDO LINHAS PARA ESCRITA" );
     int nr;
     do {
         system ( "cls" );
         Janela5 ( );
         textcolor ( LIGHTRED );
         gotoxy ( 25, 5 );
         printf ( "ARQUIVO - EXTRAINDO LINHAS PARA ESCRITA" );
         textcolor ( BROWN );
         gotoxy ( 25, 7 );
         printf ( "Programa desenvolvido por:" );
         textcolor ( WHITE );
         gotoxy ( 52, 7 );
         printf ( "Samuel Lima" );
         textcolor ( BLUE );
         gotoxy ( 34, 9 );
         printf ( "sa_sp10@hotmail.com" );
         textcolor ( LIGHTBLUE );
         gotoxy ( 24, 11 );
         printf ( "Digite 0 para sair ou 1 para continuar " );
         textcolor ( LIGHTRED );
         scanf ( "%d", &nr );
         fflush ( stdin );
         fflush ( stdin );
         if ( nr == 0 ) {
              textcolor ( LIGHTRED );
              gotoxy ( 36, 18 );
              printf ( "MUITO OBRIGADO" );
              getche ( );
              exit ( 0 );
         } else if ( nr == 1 ) {
              return 1;
         }
         textcolor ( YELLOW );
         gotoxy ( 36, 16 );
         printf ( "\aopcão errada!" );
         getche ( );
     } while ( 1 );
     return 1;
}
int abr_apag_arq ( char abre_arq [ ] ) {
     FILE *arq;
     FILE *a_q;
     int linha = 1;
     int i, real, ini, fim;
     char strings [ 100 ];
     arq = fopen ( "C:\\Users\\SAMUEL 64SP1\\Desktop\\Arquivos\\"
              "Hino à Bandeira Nacional.txt", "r + b" );
     if ( !arq ) {
         printf ( "Houve um erro na abertura do arquivo\n" );
         getche ( );
         return 0;
     }
     if ( ( a_q = fopen ( "C:\\Users\\SAMUEL 64SP1\\Desktop\\Arquivos\\"
              "Hino à Bandeira Nacional II.txt", "w" ) ) == 0 ) { //testa se o arquivo pode ser criado
         printf ( "Houve um erro na criação do arquivo\n" );
         getche ( );
         exit ( 0 );
     }
     system ( "cls" );
     Janela5 ( );
     textcolor ( LIGHTRED );
     gotoxy ( 25, 3 );
     printf ( "ARQUIVO - EXTRAINDO LINHAS PARA ESCRITA" );
     textcolor ( LIGHTBLUE );
     gotoxy ( 7, 5 );
     printf ( "A partir de qual linha deseja editar ? " );
     textcolor ( LIGHTRED );
     scanf ( "%d", &ini );
     fflush ( stdin );
     while ( ini < 0 || ini > 60 ) {
         textcolor ( LIGHTBLUE );
         gotoxy ( 25, 7 );
         printf ( "\aO Número inicial não Pode Ser Negativo" );
         textcolor ( LIGHTBLUE );
         gotoxy ( 25, 8 );
         printf ( "Nem Maior que 60 " );
         Sleep ( 1000 );
         gotoxy ( 25, 10 );
         textcolor ( AMARELO );
         printf ( "PRESSIONE QUALQUER TECLA" );
         getche ( );
         system ( "cls" );
         main ( );
     }
     textcolor ( LIGHTBLUE );
     gotoxy ( 7, 7 );
     printf ( "Até qual linha editaremos ? " );
     textcolor ( LIGHTRED );
     scanf ( "%d", &fim );
     fflush ( stdin );
     while ( fim < ini + 3 || fim > SI_ZE ) {
         textcolor ( LIGHTBLUE );
         gotoxy ( 7, 9 );
         printf (
                   "\aO Número final tem que ter no mínimo 10 linhas a mais que o inicial" );
         textcolor ( LIGHTBLUE );
         gotoxy ( 7, 10 );
         printf ( "e tem que ser menor que %d", SI_ZE );
         Sleep ( 1000 );
         gotoxy ( 25, 14 );
         textcolor ( AMARELO );
         printf ( "PRESSIONE QUALQUER TECLA" );
         getche ( );
         system ( "cls" );
         main ( );
     }
     real = ( fim - ini );
     textcolor ( LIGHTBLUE );
     gotoxy ( 7, 9 );
     printf ( "Você está prestes a editar " );
     textcolor ( LIGHTRED );
     printf ( " %d ", re_al );
     textcolor ( LIGHTBLUE );
     printf ( " linhas do arquivo " );
     rewind ( arq );
     textcolor ( LIGHTBLUE );
     gotoxy ( 7, 11 );
     printf ( "Agora digite" );
     textcolor ( LIGHTRED );
     printf ( " %d ", re_al );
     textcolor ( LIGHTBLUE );
     printf ( "linhas para editar o arquivo " );
     for ( i = 0; !feof ( arq ); i++ ) {
         memset ( strings, EDITA, 100 );
         fgets ( strings, 101, arq );
         if ( linha > ini && linha < fim + 1 ) {
              gotoxy ( 7, i + 9 );
              gets ( strings ); // lê string do teclado
              fprintf ( a_q, "%s", strings ); // escreve string no arquivo
              fputc ( '\n', a_q );
              linha = linha + 1;
              continue;
         }
         linha = linha + 1;
         fputs ( strings, a_q );
     }
     textcolor ( LIGHTBLUE );
     gotoxy ( 7, 21 );
     printf ( "As" );
     textcolor ( LIGHTRED );
     printf ( " %d ", re_al );
     textcolor ( LIGHTBLUE );
     printf ( " frases foram inseridas no arquivo " );
     fclose ( arq );
     fclose ( a_q );
     gotoxy ( 30, 23 );
     textcolor ( AMARELO );
     printf ( "PRESSIONE QUALQUER TECLA" );
     getche ( );
     return ( real );
     return ( 0 );
}
int main ( void ) {
     int c = 0, i, n;
     continuando ( );
     n = abr_apag_arq ( "louvor II.txt" );
     char **no_me;
     no_me = ( char** ) malloc ( SI_ZE * sizeof(char*) );
     FILE *file;
     FILE *arq;
     file = fopen ( "C:\\Users\\SAMUEL 64SP1\\Desktop\\Arquivos\\"
              "Hino à Bandeira Nacional II.txt", "r + b" );
     if ( !file ) {
         printf ( "Houve um erro na abertura do arquivo\n" );
         getche ( );
         return 0;
     }
     arq = fopen ( "C:\\Users\\SAMUEL 64SP1\\Desktop\\Arquivos\\"
              "Hino à Bandeira Nacional.txt", "r + b" );
     if ( !arq ) {
         printf ( "Houve um erro na abertura do arquivo\n" );
         getche ( );
         return 0;
     }
     system ( "cls" );
     Janela5 ( );
     textcolor ( LIGHTRED );
     gotoxy ( 25, 3 );
     printf ( "ARQUIVO - EXTRAINDO LINHAS PARA ESCRITA" );
     textcolor ( LIGHTBLUE );
     gotoxy ( 5, 5 );
     printf ( "Veja abaixo o arquivo original" );
     printf ( "\n\n" );
     for ( i = 0; i < SI_ZE; i++ ) {
         no_me [ c ] = ( char* ) malloc ( SI_ZE * sizeof(char*) );
         fgets ( no_me [ i ], 60, arq );
         textcolor ( LIGHTRED );
         gotoxy ( 5, i + 7 );
         printf ( "%d ", c++ );
         textcolor ( WHITE );
         printf ( "%s ", no_me [ i ] );
     }
     textcolor ( LIGHTBLUE );
     gotoxy ( 45, 5 );
     printf ( "Abaixo com as" );
     textcolor ( LIGHTRED );
     printf ( " %d ", n );
     textcolor ( LIGHTBLUE );
     printf ( "linhas editadas" );
     printf ( "\n\n" );
     c = 0;
     for ( i = 0; i < SI_ZE; i++ ) {
         no_me [ c ] = ( char* ) malloc ( SI_ZE * sizeof(char*) );
         fgets ( no_me [ i ], 60, file );
         textcolor ( LIGHTRED );
         gotoxy ( 40, i + 7 );
         printf ( " %d ", c++ );
         textcolor ( WHITE );
         printf ( " %s ", no_me [ i ] );
     }
     fclose ( arq );
     fclose ( file );
     Sleep ( 1000 );
     textcolor ( LIGHTRED );
     gotoxy ( 36, 24 );
     printf ( "MUITO OBRIGADO" );
     getche ( );
}

Nenhum comentário:

Postar um comentário

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