1
I’m a beginner in programming, and I have a paper due in college. In it I have to register clients using files, and then do a search in the file and print on the screen the data of the registered client. However, I am not able to perform the research (fourth block of codes ERROR):
//estrutura >1<
typedef struct FUNCIONARIO
{
char nome_fun[45];
char tel_fun[15];
char funcao[45];
char sal_fun[10];
char tipo_fun[15];
int cod_fun;
}FUNCIONARIO;
// Chamada da funcao de cadastro do funcionario >2<
FUNCIONARIO funcionario;
FILE * arq;
if((arq = fopen("CadastroFuncionarios.txt", "a"))==NULL) exit(1);
funcionario = cadastro_funcionario();
if (fwrite(&funcionario, sizeof(FUNCIONARIO), 1, arq)!=1) break;
fflush(arq);
fseek(arq, 0, 0);
fclose(arq);
// funcao de cadastro do funcionario >3<
FUNCIONARIO cadastro_funcionario()
{
FUNCIONARIO funcionario;
FILE *arq;
arq = fopen("CadastroFuncionarios.txt", "r");
int cont;
fseek(arq, 0, 2);
cont = ftell(arq)/sizeof(FUNCIONARIO);
fclose(arq);
// gera cod_funcionario automatico
system ("cls");
printf("*********CADASTRO DE FUNCIONARIO**********");
printf ("\n\nPreencha os dados para cadastro do Funcionario:");
funcionario.cod_fun = cont;
fprintf(arq, "%d", funcionario.cod_fun);
fprintf(arq, "|");
printf ("\nNOME: ");
setbuf(stdin, NULL);
gets (funcionario.nome_fun);
fputs(funcionario.nome_fun, arq);
fputs("|",arq);
printf ("TELEFONE: ");
setbuf(stdin, NULL);
gets (funcionario.tel_fun);
fputs(funcionario.tel_fun, arq);
fputs("|",arq);
printf ("FUNCAO: ");
setbuf(stdin, NULL);
gets (funcionario.funcao);
fputs(funcionario.funcao, arq);
fputs("|",arq);
printf ("SALARIO: ");
setbuf(stdin, NULL);
gets (funcionario.sal_fun);
fputs(funcionario.sal_fun, arq);
fputs("|",arq);
printf ("TIPO (Temporario ou Fixo): ");
setbuf(stdin, NULL);
gets (funcionario.tipo_fun);
fputs(funcionario.tipo_fun, arq);
fputs("|",arq);
fputs("\n", arq);
system("cls");
rewind(stdin);
return funcionario;
}
// pesquisa de funcionario - **ERRO** >4<
FUNCIONARIO funcionario; // struct utilizada para o cadastro e impressao na tela
FILE * arq;
if ((arq = fopen("CadastroFuncionarios.txt", "r"))==NULL) exit(1);
char array[1000], nome[30];
int i;
long nrec;
printf ("\n");
printf("**********FUNCIONARIOS CADASTRADOS**********\n\n");
printf ("Informe o nome do Funcionario: ");
setbuf(stdin, NULL);
scanf ("%s", &nome);
fscanf (arq, "%s", array);
nrec = ftell(arq)/sizeof(FUNCIONARIO);
for(i=0; i<nrec; i++)
{
if(strstr(array, nome)!=NULL)
{
pesquisa_funcionario(funcionario); // funcao para impressao na tela
break;
}
else
{
printf ("Funcionario nao cadastrado!");
}
}
fclose(arq);
break;
// funcao para impressao dos dados >5<
void pesquisa_funcionario (FUNCIONARIO funcionario)
{
printf ("Codigo: %d | ", funcionario.cod_fun);
printf ("Nome: %s | ", funcionario.nome_fun);
printf ("Telefone: %s | ", funcionario.tel_fun);
printf ("Funcao: %s | ", funcionario.funcao);
printf ("Salario: %s | ", funcionario.sal_fun);
printf ("Tipo: %s | ", funcionario.tipo_fun);
printf ("\n");
}
I understand that without posting the full code, it is not possible to execute, but I think it is too long to put here.
What is the file format Register employees.txt?
– Marcelo Shiniti Uchimura
In the input? If it’s, text, I used "a" mode, so I could add more data.
– Joao
What is being recorded inside Register employees.txt? Put a sample there.
– Marcelo Shiniti Uchimura
I added the struct and the data input part.
– Joao
Why are you closing the file halfway through
cadastro_funcionario()
– Marcelo Shiniti Uchimura
That part opens the file to count how many entries there are, and generates the 'cont' to get the cod_funcio "automatico". It opens in "r" mode, I don’t know why it’s in "a" mode there, but I’ve corrected.
– Joao
Okay, now run the program, please.
– Marcelo Shiniti Uchimura
Yes. In this section (search) it does not execute anything. I used the system ("pause"). And apparently, it does not enter the for. Error Codeblocks does not accuse.
– Joao