You are on page 1of 4

#include <stdlib.

h>
#include <stdio.h>
#include <string.h>
// Aluno : Bruno Henrique
//Arquivo txt
typedef struct
{
char nome[40];
char telefone[30];
} data;
char ch[1000];
void CopieArquivo( FILE *arqEntrada, FILE *arqSaida)
{
while (fgets(ch, 1000 , arqEntrada) != NULL)
fputs ( ch, arqSaida);
}

int main( void )


{
FILE *agendaarq;
FILE *lotearq;
int n1, n2, i = 0, l = 0, comp, n3, comp2;
char busca[40], del[40], buffer[100];
char nomedoarquivo[] = "agenda.txt";
char nomedolote[] = "lote.txt";
data agenda[1000];
char car[100];
lotearq = fopen(nomedolote,"w+");
if ( ( agendaarq = fopen( nomedoarquivo, "r") ) )
{
fgets( buffer, 100, agendaarq ); // lera a primeira linha em branco, des
cartando o conteudo
while( ! feof( agendaarq ) )
{
fgets( buffer, 100, agendaarq ); // leitura do nome
strcpy( agenda[i].nome, &buffer[6]); // copia o nome
agenda[i].nome[strlen(agenda[i].nome)-1] = '\0';
fgets( buffer, 100, agendaarq ); // leitura do telefone
strcpy( agenda[i].telefone, &buffer[10]); // copia o telefone
agenda[i].telefone[strlen(agenda[i].telefone)-1] = '\0';
fgets( buffer, 100, agendaarq ); // leitura da linha com tracos, e
descarta o conteudo
i++;
}
fclose(agendaarq);
}

do
{
printf("\nMenu\n1-Adicionar Contato.\n2-Lista de Contatos.\n3-Excluir Co
ntatos.\n4-Sair e gerar arquivo.\nDigite o numero da opcao desejada: ");
scanf("%d", &n1);
system("cls");
if ( n1 == 1 )
{
printf("\nDigite o nome da pessoa: ");
fflush(stdin);
gets(agenda[i].nome);
printf("\nDigite o numero do telefone. Exemplo: (Codigo de Area)1234
-5678.\n");
fflush(stdin);
gets(agenda[i].telefone);
i++;
//for ( l = 0; l < i; l++ )
do
{
lotearq = fopen(nomedolote,"a");
fprintf( lotearq, "\nNome: %s\nTelefone: %s\n------------------------------------------", agenda[l].nome, agenda[l].telefone );
fclose( lotearq );
l++;
}while (l < i) ;
printf("\n");
system("pause");
system("cls");
}
if ( n1 == 2 )
{
printf("\nLista de Contatos\n1-Localizar por nome.\n2-Exibir todos.\
nDigite o numero da opcao desejada: ");
scanf("%d", &n2);
system("cls");
if ( n2 == 1 )
{
printf("\nDigite o nome do contato: ");
fflush(stdin);
gets(busca);

for ( l = 0; l < i; l++ )


{
comp = strcmp( busca, agenda[l].nome );
if ( comp == 0 )
printf ("\nNome do contato: %s\nTelefone do contato: %s\
n", agenda[l].nome, agenda[l].telefone);
}
printf("\n");
system("pause");
system("cls");
}
if ( n2 == 2 )

{
for ( l = 0; l < i; l++ ){
printf("\nNome: %s\nTelefone: %s\n------------------------------------------", agenda[l].nome, agenda[l].telefone);
}
printf("\n");
system("pause");
system("cls");
}
}
if ( n1 == 3 )
{
printf("\nOpcoes disponiveis.\n1-Apagar um contato.\n2-Apagar todos.
\nDigite o numero da opcao desejada: ");
scanf("%d", &n3);
system("cls");
if ( n3 == 1 )
{
printf("\nDigite o nome do contato que deseja apagar: ");
fflush(stdin);
gets(del);
for ( l = 0; l < i; l++ )
{
comp2 = strcmp( del, agenda[l].nome );
if ( comp2 == 0 )
{
printf("\nA entrada na agenda com o nome %s foi deletada
.\n", agenda[l].nome);
strcpy( agenda[l].nome, "");
strcpy( agenda[l].telefone, "");
//CONSERTANDO SAPOHA
for(int h = l; h < i; h++){
agenda[h] = agenda[h+1];
}
i--;
//CONSERTANDO SAPOHA
lotearq =fopen(nomedolote,"w+");
for ( l = 0; l < i; l++ )
{
lotearq = fopen(nomedolo
te,"a");
fprintf( lotearq, "\nNom
e: %s\nTelefone: %s\n-------------------------------------------", agenda[l].nom
e, agenda[l].telefone );
fclose( lotearq );
}
}
}
printf("\n");
system("pause");
system("cls");
}
if ( n3 == 2 )
{
for ( l = 0; l < i; l++ )

{
strcpy( agenda[l].nome, "");
strcpy( agenda[l].telefone, "");
i = 0;
}
printf("\nTodos os registros foram deletados.\n");
lotearq = fopen(nomedolote,"w+");
for ( l = 0; l < i; l++ )
{
lotearq = fopen(nomedolo
te,"w");
fprintf( lotearq, "\nNom
e: %s\nTelefone: %s\n-------------------------------------------", agenda[l].nom
e, agenda[l].telefone );
fclose( lotearq );
}
printf("\n");
system("pause");
system("cls");
}
}
}
while ( n1 != 4 );
agendaarq = fopen(nomedoarquivo,"w+"); fclose(agendaarq); // Apenas para
limpar o arquivo
//for ( l = 0; l < i; l++ )
//{
//
agendaarq = fopen(nomedoarquivo,"a");
//
fprintf( agendaarq, "\nNome: %s\nTelefone: %s\n------------------------------------------", agenda[l].nome, agenda[l].telefone );
//
fclose( agendaarq );
//}
agendaarq = fopen(nomedoarquivo,"a");
lotearq = fopen(nomedolote,"r+");
CopieArquivo(lotearq , agendaarq);
//do{
// fputs ( ch, agendaarq);
//}while(fgets(ch, 1000 , lotearq) != NULL);
fclose(lotearq);
fclose(agendaarq);
lotearq =fopen(nomedolote,"w+"); fclose(lotearq);
getc(stdin);
return 0;
}

You might also like