Skip to content

Instantly share code, notes, and snippets.

@Supitto
Last active August 31, 2018 11:38
Show Gist options
  • Select an option

  • Save Supitto/b9da5a79bb1f68497a4417c08d5946ea to your computer and use it in GitHub Desktop.

Select an option

Save Supitto/b9da5a79bb1f68497a4417c08d5946ea to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <string.h>
//Prototipos
void limpa_memoria();
int grava_bloco();
//As variaveis abaixo só foram declaradas em escopo global para não ocuparem a
//pilha, se eu não o fizesse, o compilador faria ¯\_(ツ)_/¯
FILE * dat_file;
//Os text_buffer são para eventuais manipulações de strings
char text_buffer_1 [201];
char text_buffer_2 [201];
char safe_text_buffer[205]; //contem bytes adicionais para evitar buffer overflow
//Como o maior valor de registro é 511, podemos resevar essa memoria
char register_string [512];
//O bloco de memoria de 512 bytes sera simulado com um buffer de mesmo tamanho
char memory_block [512];
//Algumas variaveis de controle
int bytes_ocupados_em_memory = 0; //clareza é a melhor documentação
int block_offset = 0; //marca em qual bloco o programa se encontra
//Algumas definições convenientes
char separa_campo [2] = {13, 0};
char separa_registro [2] = {10, 0};
int main(void)
{
//Primeiro recebemos o nome do arquivo sem extensão
fscanf(stdin, "%s", text_buffer_1);
//Copiamos a string para um buffer maior
strcpy(safe_text_buffer, text_buffer_1);
//Concatenamos a extensão
strcat(safe_text_buffer, ".dat");
//Agora podemos abrir o arquivo em modo overwrite
dat_file = fopen(safe_text_buffer, "w");
//A proxima entrada valida é a quantidade de registros
int restantes; //instancia na pilha
fscanf(stdin, "%i", &restantes);
int len;
for(; restantes > 0; restantes--)//não queremos ocupar a pilha com inutilidades
{
//recebe RA
fscanf(stdin, "%s", &text_buffer_1);
strcpy(register_string, text_buffer_1);
strcat(register_string, separa_campo);
//recebe Nome
fscanf(stdin, "%s", &text_buffer_1);
strcpy(register_string, text_buffer_1);
strcat(register_string, separa_campo);
//recebe Curso
fscanf(stdin, "%s", &text_buffer_1);
strcpy(register_string, text_buffer_1);
strcat(register_string, separa_campo);
//recebe Ano
fscanf(stdin, "%s", &text_buffer_1);
strcpy(register_string, text_buffer_1);
strcat(register_string, separa_registro);
//Agora descobrimos o tamanho do registro
len = strlen(resgister_string);
//510 porque precisamos contar os dois bytes no final do registro
if(len > 510 - bytes_ocupados_em_memory)
{
grava_bloco();
limpa_bloco();
}
strcat(memory_block, register_string);
bytes_ocupados_em_memory += len;
}
if(bytes_ocupados_em_memory != 0)
{
grava_bloco();
limpa_bloco();
}
fclose(dat_file);
//Termina a população dos dados
return 0;
}
void limpa_memoria()
{
int i;
for(i = 0; i < 512; i++)
{
memory_block[i] = 0;
}
bytes_ocupados_em_memory = 0;
block_offset++;
}
int grava_bloco()
{
//Marcamos o final do arquivo
memory_block[511] = bytes_ocupados_em_memory & 0xFF;
memory_block[510] = bytes_ocupados_em_memory >> 8 & 0xFF;
//Gravamos o arquivo
fwrite(memory_block, 512, 1, dat_file);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment