Created
March 14, 2016 04:16
-
-
Save CleitonDeLima/42b2470b9acd1f395bf8 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
/* | |
* developed by: Cleiton de Lima Pinto | |
* dataBase II | |
*/ | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
typedef struct metadados Metadados; | |
typedef struct metadados{ | |
char nameAttr[50]; | |
char tpAttr; | |
int sizeAttr; | |
} Metadados; | |
char* getString(FILE *b){ | |
char c, aux[50] = "", result[50]; | |
int j; | |
c = getc(b); | |
for(j = 0; c != '\0'; j++){ | |
aux[j] = c; | |
c = getc(b); | |
} | |
return strcpy(result, aux); | |
} | |
void imprimeData(Metadados metadados[], int QT_ATTR, FILE *arq){ | |
int i, Integer = 0; | |
double Double = 0; | |
char String[50]; | |
for(i = 0; i < QT_ATTR; i++){ | |
printf("%-25s", metadados[i].nameAttr); | |
} | |
printf("\n----------------------------------------------------------------------------------------------------------------\n"); | |
while(!feof(arq)){ | |
for(i = 0; i < QT_ATTR; i++) { | |
if (metadados[i].tpAttr == 'C'){ | |
if (fread(&String , metadados[i].sizeAttr, 1, arq)) | |
printf("%-25s", String); | |
} | |
else if (metadados[i].tpAttr == 'I'){ | |
if (fread(&Integer , metadados[i].sizeAttr, 1, arq)) | |
printf("%-25d", Integer); | |
} | |
else if (metadados[i].tpAttr == 'D'){ | |
if (fread(&Double , metadados[i].sizeAttr, 1, arq)) | |
printf("%-25.2lf", Double); | |
} | |
} | |
printf("\n"); | |
} | |
} | |
void leMetaData(FILE *arq, FILE *arq2){ | |
int QT_ATTR, i; | |
//Quantidade de atributos | |
fread( &QT_ATTR, sizeof(int), 1, arq); | |
/*armazena os metadados variando a Quantidade | |
do primeiro valor do arquivo */ | |
Metadados novo[QT_ATTR]; | |
for(i = 0; i < QT_ATTR; i++){ | |
strcpy( novo[i].nameAttr, getString(arq)); | |
fread( &novo[i].tpAttr, sizeof(char), 1, arq); | |
fread( &novo[i].sizeAttr, sizeof(int), 1, arq); | |
} | |
imprimeData(novo, QT_ATTR, arq2); | |
} | |
void printHelp(){ | |
printf("=================HELP=================\n"); | |
printf("./nome_programa [OPTION1] [OPTION2]\n\n"); | |
printf("OPTION1\n"); | |
printf("/caminho_arquivo_meta.dat\n\n"); | |
printf("OPTION2\n"); | |
printf("/caminho_arquivo_data.dat\n\n"); | |
printf("======================================\n"); | |
} | |
int main(int argc, char *argv[]){ | |
//open file | |
if(argc == 1){ | |
printHelp(); | |
exit(1); | |
} | |
else if(argc == 3){ | |
FILE *arq = fopen( argv[1], "rb"); | |
FILE *arq2 = fopen( argv[2], "rb"); | |
if( (arq == NULL) || (arq2 == NULL) ){ | |
printf("Arquivo(s) não encontrado(s)!\n"); | |
exit(1); | |
} | |
leMetaData(arq, arq2); | |
} | |
else{ | |
printf("Paramentros invalidos!\n"); | |
exit(1); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment