Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #pragma pack(1) //mudar a forma de leitura de bytes (mais eficiente)
- typedef struct {
- int id;
- short age;
- float height;
- float weight;
- char gender;
- } Record;
- abrir_arquivo(FILE **file, Record *ptr)
- {
- if( !(*file = fopen("records.dat","rb")) ) // rb = read binary (abrir arquivo de binário para ler)
- {
- printf("\n\t\t*** ERROR: FILE NOT FOUND ***\n\n");
- exit (1);
- }
- }
- mostrar_tudo(FILE **file, Record *ptr)
- {
- int count = 0, i = 0;
- abrir_arquivo(file, ptr);
- system("cls");
- printf("\n\t\tEXIBICAO DE TODOS OS DADOS");
- fread(&count, sizeof(int), 1, *file);
- printf("\n\n\tAGE WEIGHT HEIGHT GENDER\n");
- do
- {
- fread(ptr, sizeof(Record), 1, *file);
- printf("\n %5d %5.2f %4.2f %4c\n", ptr->age, ptr->height, ptr->weight, ptr->gender); //exibindo dados do arquivo
- i++;
- }while(i < count);
- fclose(*file); //fechando meu arquivo
- printf("\n\nAperte Enter para voltar ao Menu...");
- getche();
- }
- soma_pesos(FILE **file, Record *ptr)
- {
- int count = 0, i = 0;
- abrir_arquivo(file, ptr);
- system("cls");
- float sum_weight;
- fread(&count, sizeof(int), 1, *file);
- do
- {
- fread(ptr, sizeof(Record), 1, *file);
- sum_weight += ptr->height;
- i++;
- }while(i < count);
- printf("\n\n\tSOMA DE TODOS OS PESOS: %.2f", sum_weight);
- fclose(*file); //fechando meu arquivo
- printf("\n\nAperte Enter para voltar ao Menu...");
- getche();
- }
- int maior_altura(FILE **file, Record *ptr)
- {
- int count = 0, i = 0;
- abrir_arquivo(file, ptr);
- system("cls");
- float height;
- fread(&count, sizeof(int), 1, *file);
- do
- {
- fread(ptr, sizeof(Record), 1, *file);
- if(ptr->weight > height)
- height = ptr->weight; //note que está invertida a leitura da altura em relação ao peso.
- i++;
- }while(i < count);
- printf("\n\n\tMAIOR ALTURA: %.2f", height);
- fclose(*file); //fechando meu arquivo
- printf("\n\nAperte Enter para voltar ao Menu...");
- getche();
- return 0;
- }
- maior_idade(FILE **file, Record *ptr)
- {
- abrir_arquivo(file, ptr);
- system("cls");
- short age;
- do
- {
- fread(ptr, sizeof(Record), 1, *file);
- if(ptr->age > age)
- age = ptr->age;
- }while(!feof(*file));
- printf("\n\n\tMAIOR IDADE: %d", age);
- fclose(*file); //fechando meu arquivo
- printf("\n\nAperte Enter para voltar ao Menu...");
- getche();
- }
- pessoas(FILE **file, Record *ptr)
- {
- abrir_arquivo(file, ptr);
- system("cls");
- int men, women;
- do
- {
- fread(ptr, sizeof(Record), 1, *file);
- if(ptr->gender == 'M' || ptr->gender == 'm')
- men++;
- else
- women++;
- }while(!feof(*file));
- printf("\n\n\td) QUANTIDADE DE HOMENS: %d", men);
- printf("\n\t QUANTIDADE DE MULHERES: %d\n\n", women);
- fclose(*file); //fechando meu arquivo
- printf("\n\nAperte Enter para voltar ao Menu...");
- getche();
- }
- main()
- {
- int opcao;
- Record *ptr;
- FILE *file;
- abrir_arquivo(&ptr, &file);
- do
- {
- system("cls");
- printf("\n\t\tMENU DE OPCOES");
- printf("\n\n1-Exibir todos os dados");
- printf("\n2-Exibir soma de todos pesos");
- printf("\n3-Exibir maior altura");
- printf("\n4-Exibir maior idade");
- printf("\n5-Exibir quantidade de homens e mulheres");
- printf("\n0-Sair");
- printf("\n\nEntre com a opcao: ");
- scanf("%d", &opcao);
- if(opcao != 0)
- {
- switch(opcao)
- {
- case 1: mostrar_tudo(file, ptr); break;
- case 2: soma_pesos(file, ptr); break;
- case 3: maior_altura(file, ptr); break;
- case 4: maior_idade(file, ptr); break;
- case 5: pessoas(file, ptr); break;
- default: printf("\nOpcao invalida"); break;
- }
- }
- }while(opcao != 0);
- }
Add Comment
Please, Sign In to add comment