0
Good evening, I’m doing a little project of writing binary files in a txt and then reading these files. But I have to read the file or list them all. How could I do a search for the necessary file? And one more question what is the feof? List the last data, I wanted to list by search, type type the name and query. 1-Let’s assume that I record the song "In the end" from Linkin park...and then record other songs. I wanted that when I pressed on consult, I typed the name of the song in case "In the end" and returned the data only of my query
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
struct{
char nome[30];
char banda[40];
float valor;
}musica;
void escreveMusica()
{
char numstr[8];
FILE *fptr;
if((fptr=fopen("Musicas","wb"))==NULL){
printf("Não posso abrir arquivo...Musicas");//Caso nao consiga abrir
exit(1);
}
do{
fflush(stdin);
printf("\n Digite o nome da musica: ");
gets(musica.nome);
printf("\n Digite o nome da banda: ");
gets(musica.banda);
printf("\n Digite o preco: ");
gets(numstr);
musica.valor=atof(numstr);
fwrite(&musica,sizeof(musica),1,fptr);
printf("\n Adiciona outro registro no arquivo..");
}
while(getchar()=='s');
fclose(fptr);
}
void listarMusicas(){
char numstr[8];
FILE *fptr;
if((fptr=fopen("Musicas","rb"))==NULL){
printf("Nao posso abrir arquivo... Musicas");
exit(1);
}
printf("\nDados da consulta:\n");
while(fread(&musica, sizeof(musica),1,fptr)==1){//Pegar todas As
listagens
printf("\n Nome: %s\n",musica.nome);
printf("\n Banda: %s\n",musica.banda);
printf("\n Preco: %.2f\n",musica.valor);
printf("\n--------------------\n");
}
fclose(fptr);
}
void consultarMusica(){ //Lista o ultimo dado, eu queria listar por
char numstr[8]; pesquisa, tipo digitar o nome e listar
FILE *fptr;
if((fptr=fopen("Musicas","rb"))==NULL){
printf("Nao posso abrir arquivo... Musicas");
exit(1);
}
printf("\nDados da consulta:\n");
printf("\n Nome: %s\n",musica.nome);
printf("\n Banda: %s\n",musica.banda);
printf("\n Preco: %.2f\n",musica.valor);
printf("\n--------------------\n");
fclose(fptr);
}
int main()
{
int scan;
do{
printf("Selecione a opcao desejada\n1.Para comprar musica\n2.Para
consultar todas as musicas compradas\n3.Consultar Ultima
Musica\n0.Sair\n");
scanf("%d",&scan);
switch(scan){
case 1: escreveMusica();
system("cls");
break;
case 2: listarMusicas();
printf("\n");
break;
case 3: consultarMusica();
printf("\n");
break;
case 0: break;
default:
printf("Opcao Invalida");
}
}
while(scan!=0);
return 0;
}
Your question is not very clear click on Edit and describe better what you need. Like what kind of search you want to do, or what you want to seek and where you want to seek.
– Brumazzi DB
ready-made
– Anderson Henrique