Translate

quarta-feira, 4 de dezembro de 2013

Programa completo agenda usando arquivos em C++ cria, lê, busca, exclui

#include <iostream>
#include <fstream>
#include <string.h>
#include <stdio.h>
#include <conio.h>

using namespace std;

void busca(){
    char nome[100];
    char nom_concat[100] = "Nome: ";
    char nom_busca[205];
    char *result; 
    int i;
    char Linha[100];
    FILE *agenda;
   
   
    // Abre um arquivo TEXTO para LEITURA 
    agenda = fopen("j:\\Agenda.txt", "r+");
    if (agenda == NULL)  // Se houve erro na abertura 
    {    printf("Problemas na abertura do  arquivo\n");    
        
    }
   
    cout << "Digite o nome p/ buscar: ";
    cin >> nome;
   
    strcat( nome, "\n");
    strcat( nom_concat, nome );
   
    while (!feof(agenda))
    {
        // Lê uma linha (inclusive com o '\n')     
        result = fgets(Linha, 100, agenda); 
   
        // o 'fgets'  lê até 99 caracteres ou até o '\n'     
        if(strcmp(Linha,nom_concat)==0){
          printf("Usuário encontrado \n");
          printf("Linha %d : %s",i,Linha);
          getch();
        }
    }
       
    fclose(agenda);
    getch();   
}


#include <iostream>
#include <fstream>
#include <string.h>
#include <stdio.h>
#include <conio.h>

using namespace std;

void exclui(){
    int r;
    char sim[2];
    FILE *agenda;
    cout << "Deseja realmente excluir o arquivo? (s - n): ";
    cin >> sim;

    if(stricmp(sim,"s") == 0){
        remove("j:\\Agenda.txt");
    }
}


#include <iostream>
#include <fstream>
#include <string.h>
#include <stdio.h>
#include <conio.h>

using namespace std;

void le(){
    FILE *agenda;
    char ch;
    ifstream fin("j:\\Agenda.txt"); // Abre arquivo para leitura
    // Enquanto não for fim de arquivo:
    while(fin.get(ch)){ // lê um caracter do arquivo
    cout << ch;}
}


#include <iostream>
#include <fstream>
#include <string.h>
#include <stdio.h>
#include <conio.h>
#include <cstdlib>
#include <cstring>
#include <stdlib.h>

using namespace std;

struct contato{
    char nome[40];
    char end[100];
    char tel[15];
};

void busca();
void le();
void exclui();

int main(int, char **) {
   
    int opcao;
    FILE *agenda;
   
    do{
   
        cout <<"\n\n|------------Menu de opções------------|\n\n";
        cout <<"1 - Criar nova agenda\n";
        cout <<"2 - Cadastrar um contato\n";
        cout <<"3 - Excluir a agenda\n";
        cout <<"4 - Buscar um contato pelo nome\n";
        cout <<"5 - Imprimir a agenda\n";
        cout <<"6 - Encerrar o programa\n\n";
        cout <<"\n|--------------------------------------|\n\n\n";
        cout <<"Informe uma opção:  ";
        cin >> opcao;
      
            switch(opcao){
              
                case 1:
                    agenda = fopen ("i:\\Agenda.txt", "w");
                                     
                    if (agenda == NULL) {
                       printf ("Houve um erro ao abrir o arquivo.\n");
                       return 1;
                    }
                  
                    printf ("\n\nAgenda criada com sucesso!\n\n");
                    fclose(agenda);
                    break;
                  
                case 2:
                    int x;
                    agenda = fopen ("j:\\Agenda.txt", "a");
                  
                    if (agenda == NULL) {
                       printf ("Houve um erro ao abrir o arquivo.\n");
                       return 1;
                    }
                  
                    cout << "\nInforme a quantidade que deseja cadastrar: ";
                    cin >> x;
                  
                    for(int i=0; i<x; i++){
                        contato cont[x];
                        cout<<"\nInforme o nome: ";
                        cin>> cont[i].nome;
                        cout<<"Informe o endereço: ";
                        cin>> cont[i].end;
                        cout<<"Informe o telefone: ";
                        cin>> cont[i].tel;
                        fprintf(agenda,"\nNome: %s\n",cont[i].nome);
                        fprintf(agenda,"Endereço: %s\n",cont[i].end);
                        fprintf(agenda,"Telefone: %s\n",cont[i].tel);
                    }
                    fclose (agenda);
                    break;
                  
              
                case 3:
                    exclui();
                    break;
                  
                      
                case 4:  
                    busca();
                      break;
              
                  
                case 5:
                    le();
                    break;  
      
            }  
    }    while(opcao != 6);
        cout << "\n\n\n\ Programa encerrado!";          
}



Nenhum comentário:

Postar um comentário