Created
November 15, 2013 18:37
-
-
Save rat/7489383 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "file.h" | |
/** | |
* Cria um arquivo binário com a extensão .dat | |
* de forma segura. A função verifica a existência | |
* do arquivo antes de tentar criá-lo, assim evita | |
* a sobrescrita de dados. | |
* | |
* @param char *file string com o nome do arquivo que você quer criar. | |
* | |
* @return void | |
* | |
* @author Renato Tavares <dr.renatotavares@gmail.com> | |
* | |
*/ | |
void makeFile(char *file) { | |
if (access(file, F_OK) != -1) { | |
// arquivo já existe não preciso criar novamente | |
} else { | |
FILE *filePtr; | |
if ((filePtr = fopen(file, "w")) == NULL) { | |
printf("Arquivo %s não pode ser criado.", file); | |
exit(EPERM); | |
} | |
fclose(filePtr); | |
} | |
} | |
/** | |
* Insere um novo registro no arquivo especificado. A função SEMPRE | |
* insere o novo registro no final do arquivo, assim os dados | |
* anteriores sempre são preservados. | |
* | |
* @param char *file string com o nome do arquivo que você quer inserir. | |
* @param void *data struct com os dados que você deseja inserir. | |
* @param size_t len tamanho da struct com os dados a inserir. | |
* | |
* @return void | |
* | |
* @author Renato Tavares <dr.renatotavares@gmail.com> | |
* | |
*/ | |
void fileInsert(char *file, void *data, size_t len) { | |
FILE *filePtr; | |
if ((filePtr = fopen(file, "rb+")) == NULL) { | |
printf("Arquivo não pode ser aberto."); | |
exit(EPERM); | |
} else { | |
fseek(filePtr, 0L, SEEK_END); | |
fwrite(data, len, 1, filePtr); | |
fclose(filePtr); | |
} | |
} | |
/** | |
* Lista todos os registros do arquivo especificado. | |
* | |
* @param char *file string com o nome do arquivo que você quer listar. | |
* | |
* @return void | |
* | |
* @author Renato Tavares <dr.renatotavares@gmail.com> | |
* | |
*/ | |
void fileListAllClients(char *file) { | |
system("cls"); | |
FILE *filePtr; | |
Cliente reg; | |
if ((filePtr = fopen(file, "rb+")) == NULL) { | |
printf("Arquivo não pode ser aberto."); | |
exit(EPERM); | |
} else { | |
printf("LISTAGEM DE CLIENTES\n\n\n"); | |
printf("Codigo Nome \n"); | |
printf("----------------------------------------\n\n"); | |
while ( !feof(filePtr) ) { | |
fread(®, sizeof (reg), 1, filePtr); | |
if ( !feof(filePtr) ) { | |
if(reg.ativo != 0){ | |
printf("%d %s\n", reg.codigo, reg.nome); | |
} | |
} | |
} | |
fclose(filePtr); | |
} | |
} | |
/** | |
* Lista todos os registros do arquivo especificado. | |
* | |
* @param char *file string com o nome do arquivo que você quer listar. | |
* | |
* @return void | |
* | |
* @author Renato Tavares <dr.renatotavares@gmail.com> | |
* | |
*/ | |
void fileListAllCars(char *file) { | |
system("cls"); | |
FILE *filePtr; | |
Carro reg; | |
if ((filePtr = fopen(file, "rb+")) == NULL) { | |
printf("Arquivo não pode ser aberto."); | |
exit(EPERM); | |
} else { | |
printf("LISTAGEM DE CARROS\n\n\n"); | |
printf("Codigo Nome Marca Valor \n"); | |
printf("----------------------------------------------------\n\n"); | |
while ( !feof(filePtr) ) { | |
fread(®, sizeof (reg), 1, filePtr); | |
if ( !feof(filePtr) ) { | |
printf("%d %s %s %.2f\n", reg.id, reg.nome, reg.marca, reg.valor); | |
} | |
} | |
fclose(filePtr); | |
} | |
} | |
/** | |
* Lista todos os registros do arquivo especificado. | |
* | |
* @param char *file string com o nome do arquivo que você quer listar. | |
* | |
* @return void | |
* | |
* @author Renato Tavares <dr.renatotavares@gmail.com> | |
* | |
*/ | |
void fileListAllReturns(char *file) { | |
system("cls"); | |
FILE *filePtr; | |
Locacao reg; | |
if ((filePtr = fopen(file, "rb+")) == NULL) { | |
printf("Arquivo não pode ser aberto."); | |
exit(EPERM); | |
} else { | |
printf("LISTAGEM DE DEVOLUÇÕES\n\n\n"); | |
printf("Codigo Nome do cliente Nome do carro Marca do carro Valor da locação\n"); | |
printf("----------------------------------------------------\n\n"); | |
while ( !feof(filePtr) ) { | |
fread(®, sizeof(reg), 1, filePtr); | |
if ( !feof(filePtr) ) { | |
printf("%d %s %s %s %.2f\n", reg.id, reg.cli_nome, reg.car_nome, reg.car_marca, reg.car_valor); | |
} | |
} | |
fclose(filePtr); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment