0
Hi, I need you to help me... I did this algorithm to register the "interviews" of employees, and whenever I want to fetch an employee, I simply put his name and all the data relating to him appear. The program is working properly, however, the ".txt file" which in my case is "reply.txt" is saving as BINARY...(And is with some strange symbols)... helppp
CODE:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
typedef struct contato CONTATO;
struct contato{
char nome[30];
char sexo[15];
char idade[5];
float renda;
char fumante[5];
char esportes[5];
char atividadef[5];
};
void cabecalho();
void cabecalho2();
void inputCTT();
void pesquisar();
int main(){
int opcao;
do{
cabecalho();
scanf("%d", &opcao);
switch (opcao){
case 1:
inputCTT();
break;
case 2:
pesquisar();
break;
case 3:
printf("--------------------\n");
printf("Volte sempre! =D \n");
printf("--------------------\n");
getch();
break;
default:
printf("-----------------------\n");
printf("Opcao invalida...\n");
printf("-----------------------\n");
getch();
break;
}
}while (opcao != 3);
}
void cabecalho(){
system("cls");
printf("\n\t\t\t\t ALCATRAZ LTDA\n");
printf("\t\t\t------------------------------\n");
printf("\t\xC9\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xBB\n");
printf("\t\xBA Para realizar uma nova entrevista: Digite [1] \xBA\n");
printf("\t\xBA \xBA\n");
printf("\t\xBA Para realizar uma busca no banco de dados: Digite [2] \xBA\n");
printf("\t\xBA \xBA\n");
printf("\t\xBA Para sair: Digite [3] \xBA\n");
printf("\t\xC8\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xBC\n");
}
void cabecalho2(){
system("cls");
printf("\n\t\t\t\t ALCATRAZ LTDA\n");
printf("\t\t\t------------------------------\n");
}
void inputCTT(){
FILE* resposta;
CONTATO ctt;
resposta = fopen("resposta.txt", "a");
if (resposta == NULL){
printf("Erro na abertura do arquivo...\n");
}
else{
do{
cabecalho2();
fflush(stdin);
printf("\n");
printf("Nome: ");
gets(ctt.nome);
fflush(stdin);
printf("Sexo: [Masculino] / [Feminino] / [Outro]: ");
gets(ctt.sexo);
fflush(stdin);
printf("Idade: ");
gets(ctt.idade);
fflush(stdin);
printf("Renda: ");
scanf("%f", &ctt.renda);
fflush(stdin);
printf("Fumante: [Sim/Nao]: ");
gets(ctt.fumante);
fflush(stdin);
printf("Gosta de esportes: [Sim/Nao]: ");
gets(ctt.esportes);
fflush(stdin);
printf("Pratica atividade fisica: [Sim/Nao]: ");
gets(ctt.atividadef);
fwrite(&ctt, sizeof(CONTATO),1, resposta);
printf("\n\nDeseja continuar: [S/N]?\n");
}
while(getche() != 'n');
fclose(resposta);
}
}
void pesquisar(){
FILE* resposta;
CONTATO ctt;
char nome[30];
cabecalho2();
resposta = fopen("resposta.txt", "rt");
if (resposta == NULL){
printf("Erro na abertura do arquivo...\n");
}else{
fflush(stdin);
printf("Digite o nome a pesquisar: ");
gets(nome);
while(fread(&ctt, sizeof(CONTATO), 1, resposta)==1 ){
if (strcmp(nome, ctt.nome)==0){
printf("\nNome: %s\n", ctt.nome);
printf("Sexo: %s\n", ctt.sexo);
printf("Idade: %s\n", ctt.idade);
printf("Renda Mensal: %.f\n", ctt.renda);
printf("Fumante: %s\n", ctt.fumante);
printf("Gosta de esportes: %s\n", ctt.esportes);
printf("Pratica atividade fisica: %s\n", ctt.atividadef);
}
}
}
fclose(resposta);
getch();
}
I used CODE::BLOCKS. I just needed the "reply.txt" to be readable...
– Gui
Do you want to
resposta.txt
can be read by notepad, for example?– sbrubes
It is already being read. But it was like binary... But the solution below was very responsive, thanks.
– Gui