sexta-feira, 20 de novembro de 2015

Arquivo - lendo e salvando em outro diretório

Algumas vêzes precisamos lê um arquivo em um diretório
e salva-lo em outro, e isto pode ser feito facilmente
seguindo o exemplo deste programa.
Duas matrizes de string foram alocadas dinâmicamente para
receber a carga de dois ponteiros para arquivo contendo
o conteúdo do arquivo, que é um código de um programa em c.

Veja abaixo imagens do programa em execução:



Veja abaixo o código do programa:

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#define SIZE 65
void Janela ( ) {
     int l, c;
     for ( l = 1; l <= 30 ; l++ ) {
         for ( c = 1; c < 80 ; c++ ) {
              gotoxy ( c , l );
              if ( l == 2 ) {
                   textattr ( 200 );
                   printf ( " " );
              }
              if ( c == 1 ) {
                   textattr ( 200 );
                   printf ( "  " );
              }
              if ( c == 79 ) {
                   textattr ( 200 );
                   printf ( "  " );
              }
         }
     }
}
void Moldura ( int a , int c , int b , int d , int e , int f ) {
     for ( a = e; a <= c ; a++ )
         for ( b = f; b <= d ; b++ ) {
              gotoxy ( b , a );
              textbackground ( WHITE );
              printf ( " " );
         }
}
int main ( ) {
     system ( "title ARQUIVO - LENDO E SALVANDO EM OUTRO DIRETÓRIO" );
     textbackground ( WHITE );
     Janela ( );
     FILE *arq;
     FILE *fp;
     int i;
     char **line;
     line = ( char** ) malloc ( SIZE * sizeof(char*) );
     char **li_ne;
     li_ne = ( char** ) malloc ( SIZE * sizeof(char*) );
     if ( ( arq = fopen ( "C:\\Users\\Lima 32 SP1\\Desktop\\Arquivos\\"
              "teste.cpp" , "r" ) ) == NULL ) {
         textbackground ( BLACK );
         textcolor ( WHITE );gotoxy ( 22 , 12 );
         printf ( "Houve um erro na abertura do arquivo" );
         getche ( );
         exit ( 1 );
     }
     if ( ( fp = fopen ( "H:\\eclipse - indigo\\PROJETOS\\main\\Release\\"
              "tes_te.cpp" , "w" ) ) == NULL ) {
         printf ( "Houve um erro na criação do arquivo" );
         getche ( );
         exit ( 1 );
     }
     for ( i = 0; i < 20 ; i++ ) {
         line [ i ] = ( char* ) malloc ( SIZE * sizeof(char*) );
         fgets ( line [ i ] , 60 , arq );
     }
     fclose ( arq );
     textbackground ( WHITE );
     textcolor ( LIGHTRED );
     gotoxy ( 22 , 3 );
     printf ( "ARQUIVO - LENDO E SALVANDO EM OUTRO DIRETÓRIO" );
     textcolor ( LIGHTBLUE );
     gotoxy ( 22 , 5 );
     printf ( "Abaixo o arquivo lido de um diretório" );
     for ( i = 0; i < 20 ; i++ ) {
         textcolor ( LIGHTRED );
         gotoxy ( 5 , i + 7 );
         printf ( "%d" , i );
         textbackground ( WHITE );
         textcolor ( BLACK );
         gotoxy ( 10 , i + 7 );
         printf ( "%s" , line [ i ] );
         fprintf ( fp , "%s" , line [ i ] );
     }
     Sleep ( 1000 );
     textcolor ( LIGHTRED );
     gotoxy ( 30 , 27 );
     printf ( "PRESSIONE QUALQUER TECLA" );
     fclose ( fp );
     getche ( );
     if ( ( fp = fopen ( "H:\\eclipse - indigo\\PROJETOS\\main\\Release\\"
              "tes_te.cpp" , "r" ) ) == NULL ) {
         system ( "cls" );
         gotoxy ( 22 , 16 );
         textcolor ( LIGHTRED );
         printf ( "Houve um erro na leitura do arquivo" );
         getche ( );
         exit ( 1 );
     }
     for ( i = 0; i < 20 ; i++ ) {
         li_ne [ i ] = ( char* ) malloc ( SIZE * sizeof(char*) );
         fgets ( li_ne [ i ] , 60 , fp );
     }
     system ( "cls" );
     system ( "Color 10" );
     Moldura ( 2 , 27 , 2 , 78 , 2 , 3 );
     textcolor ( LIGHTRED );
     gotoxy ( 22 , 3 );
     printf ( "ARQUIVO - LENDO E SALVANDO EM OUTRO DIRETÓRIO" );
     textcolor ( BLACK );
     gotoxy ( 22 , 5 );
     printf ( "Abaixo o arquivo lido de um outro diretório" );
     textcolor ( LIGHTBLUE );
     for ( i = 0; i < 20 ; i++ ) {
         textcolor ( LIGHTRED );
         gotoxy ( 5 , i + 7 );
         printf ( "%d" , i );
         gotoxy ( 10 , i + 7 );
         textcolor ( LIGHTBLUE );
         printf ( "%s" , li_ne [ i ] );
     }
     Sleep ( 1000 );
     textcolor ( LIGHTRED );
     gotoxy ( 36 , 27 );
     printf ( "MUITO OBRIGADO" );
     getche ( );
     fclose ( fp );
     free ( line );
     free ( li_ne );
     return 0;
}

Nenhum comentário:

Postar um comentário

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