How to make an appointment in C language?

Asked

Viewed 2,016 times

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;
 }
  • 1

    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.

  • ready-made

1 answer

3


If you want to fetch data on for a query within a file, I recommend using fwrite and the fread, because it will write the exact bytes to your structure, thus facilitating your search.

Entering data:

typedef struct __musica{
    char nome[64];
    char banda[64];
    float valor;
}musica;

The music structure has a fixed size, so it can be seen with the sizeof

void inserir(musica *m, FILE *f){
    fwrite(m, sizeof(musica), 1, f); // escreve a estrutura no arquivo
}

Searching for data:

musica *busca_p_nome(FILE *f, const char *nome){
    musica *m = malloc(sizeof(musica));
    fseek(f, SEEK_SET, 0); // vai até o inicio do arquivo
    while(!feof(f)){
        fread(m, sizeof(musica), 1, f); // le a estrutura do arquivo
        if(!strcmp(m->nome,nome)) // compara o nome da musica com o nome da musica desejada
             return m; // retorna a musica se encontrar
    }

    return NULL; // se não encontrar a musica retorna Nulo
}

To insert the songs without deleting the existing ones, the file must contain the parameter "a" to be opened. fopen("lista.txt", "a"), the a represents Append To File.

The feof means End Of File (the first letter represents that it belongs to the file method, so feof), when it reaches the end of the file it returns 1, Otherwise 0.

That’s why you wear !feof(arquivo) on parole.

  • Ta, let’s go and solve my error, but I want to understand the code, what exactly did you do in (File *f, const char *name) Created another pointer? And if so, what for? While the line *m = malloc(sizeof(music)); why do you need to figure out the size? (fseek SEEK_SET, 0) I don’t know what these commands are for could give me a brief explanation?

  • musica *busca_p_name(FILE *f, const char *name) I created an aeod that will fetch the music, FILE *f is the file where you will search and const char *name represents any text you want to put. # *m = malloc - You are reserving a memory space of the size of the music structure. # fseek walks through the file, SEEK_SET says it will position from the beginning of the file, and from the file you want to go.

  • Ah, all right, thank you :D

  • Dude, another thing that confused me was the typedef having almost the same name as _music and music that doesn’t change at all?

  • typedef creates a nickname, if it was just struct music, in the code it should write struct musica always, so I put a similar name and then set the nickname, or if musica is equal to struct __musica

  • I know typedef creates nickname, but got confused with that song after the keys of the struct too, I’m going into struct now so I don’t have much knowledge in the struct part that insert function I should call it in what part of the code? and to pass her parameters?

  • the struct part has no need to call anywhere else, now the method, the parameters are... the file you are saving the data and a text "Let it be" (the name of the song).

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.