How to make a query in C language?

1

Good evening, I'm making a project of writing files in binaries in a txt and then reading those files. But I have to read the file or list all. How could I do a search for the required file? And one more question what is feof? List the last given, I wanted to list by search, type type the name and consult. 1-Let's suppose that I record the song "In the end" of linkin park ... and then record other songs. I wanted it when I pressed to consult, I would type the name of the song in the case "In the end" and return the data from my query only

#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;
 }
    
asked by anonymous 03.12.2016 / 03:40

1 answer

3

If you're looking for data in a query within a file, I recommend using fwrite and fread , as it will write the exact bytes to its structure, thus easing your search.

Inserting 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 sizeof

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

Searching 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 songs without erasing existing ones, the file must contain the "a" parameter to open. fopen("lista.txt", "a") , a represents Append To File .

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

So we use !feof(arquivo) in conditional.

    
03.12.2016 / 05:05