Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
Vitor-Ribe authored Nov 28, 2024
1 parent 8e1c80b commit 23499c7
Show file tree
Hide file tree
Showing 8 changed files with 305 additions and 1 deletion.
43 changes: 43 additions & 0 deletions LAB03/Buffer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include "Buffer.h"

Buffer::Buffer(const std::string& nomeArquivo, std::ios_base::openmode modo) {
arquivo.open(nomeArquivo, modo);
if (!arquivo.is_open()) {
throw std::runtime_error("Não foi possível abrir o arquivo: " + nomeArquivo);
}
}

Buffer::~Buffer() {
if (arquivo.is_open()) {
arquivo.close();
}
}

void Buffer::escreverRegistro(const Registro& reg, bool comDescritor) {
if (comDescritor) {
std::string dados = reg.serializarDescritor();
arquivo.write(dados.c_str(), dados.size());
} else {
std::string dados = reg.serializarDelimitadores();
arquivo << dados;
}
}

bool Buffer::lerRegistro(Registro& reg, bool comDescritor) {
if (comDescritor) {
size_t tamanho;
if (!arquivo.read(reinterpret_cast<char*>(&tamanho), sizeof(size_t))) {
return false;
}
std::string dados(tamanho, '\0');
arquivo.read(&dados[0], tamanho);
reg.desserializarDescritor(dados);
} else {
std::string linha;
if (!std::getline(arquivo, linha)) {
return false;
}
reg.desserializarDelimitadores(linha);
}
return true;
}
21 changes: 21 additions & 0 deletions LAB03/Buffer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#ifndef BUFFER_H
#define BUFFER_H

#include "Registro.h"
#include <fstream>
#include <stdexcept>
#include <string>

class Buffer {
private:
std::fstream arquivo;

public:
Buffer(const std::string& nomeArquivo, std::ios_base::openmode modo);
~Buffer();

void escreverRegistro(const Registro& reg, bool comDescritor);
bool lerRegistro(Registro& reg, bool comDescritor);
};

#endif
101 changes: 101 additions & 0 deletions LAB03/Dados.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
Nome|Sobrenome|Telefone|Nascimento
Dominic Norris|Franklin|(40) 42272-1137|03/03/1971
Brian Mccarthy|Odonnell|(90) 80886-9288|13/04/1979
Garrison Witt|Robbins|(78) 22701-5394|09/09/1998
Hop Hensley|Abbott|(29) 69684-0147|01/01/1970
Keegan Richard|Wood|(76) 30723-6872|24/09/1968
Lucy Kelly|Solis|(44) 12429-1440|03/07/1997
Alana Melton|Mcintosh|(51) 47017-5770|29/07/1980
Tiger Brady|Suarez|(27) 78863-5533|13/05/1977
Vanna Mitchell|Short|(33) 79063-9463|23/09/1950
Xaviera Booker|Fletcher|(60) 81763-7288|09/09/1964
Breanna Ingram|Compton|(99) 52427-6464|26/03/1975
Octavia Davis|Mcgee|(24) 26326-7051|18/01/1983
Barbara Hopper|Mccarthy|(79) 42564-0678|09/02/1959
Leandra Winters|James|(36) 78374-7761|14/10/1952
Zachary Reese|Kline|(82) 26235-2874|20/09/1990
Madonna Whitehead|Durham|(69) 21805-2871|26/06/1950
Nigel Gardner|Bentley|(91) 42461-2037|24/07/1981
Maxwell Washington|Vance|(46) 62773-2443|25/04/1956
Leandra Cantrell|Giles|(50) 17169-1752|29/06/1958
Malachi Frost|Schultz|(14) 54966-1414|05/10/1969
Brian Benjamin|Bruce|(83) 94709-0189|25/03/1965
Perry Avila|Finch|(34) 29944-1645|17/06/1990
Yeo Glover|Bass|(99) 99554-5167|31/12/1984
Melodie Bradshaw|Joyner|(26) 85530-7695|09/03/1952
Luke Frost|May|(92) 50601-5746|09/09/1993
Ora Nichols|Barton|(63) 66524-4640|12/02/1971
Lael Vaughan|Odonnell|(46) 90664-9808|22/08/1972
Justina Curtis|Tucker|(28) 53677-4780|29/08/1959
Darius Merritt|Ferrell|(15) 19097-1444|11/07/1954
Fleur Sexton|Saunders|(42) 31828-7388|15/09/1954
Jenette Witt|Simpson|(64) 96662-8957|17/03/1987
Arden Goodman|Wheeler|(17) 80901-8955|23/09/2000
Leonard Buckner|Perez|(85) 13635-3422|27/09/2005
Oscar Madden|Gould|(65) 94393-0743|20/11/1951
Mercedes Bray|Rivas|(48) 52805-0313|18/10/1963
Jana Merritt|Mann|(36) 14948-9873|26/06/1987
Drew Price|Reyes|(77) 14719-8924|10/12/1985
Jael Weaver|Nicholson|(52) 96938-3214|18/10/1956
Marcia Strickland|Duffy|(88) 96820-5232|31/03/1973
Cassidy Carroll|Cannon|(16) 45750-7878|25/08/1976
Ayanna Fleming|Dennis|(28) 91706-1486|18/05/1970
Susan Fischer|Goodwin|(73) 63165-7146|02/03/1981
Judah Downs|Villarreal|(90) 46030-6655|22/05/1974
Kennan Bowen|Franklin|(18) 37116-0685|01/04/1964
Calvin Gay|Heath|(12) 94284-9291|16/05/1976
Vera Christian|Burton|(60) 54488-8972|18/03/2005
Richard Schultz|Morton|(12) 92844-3701|16/05/1992
Judah Wynn|Schultz|(78) 87624-3907|12/05/1998
Jordan Clements|Richards|(97) 91270-3276|16/02/1985
Beverly Gaines|Bentley|(16) 69114-5266|17/03/1997
Hedy Richards|Reilly|(23) 24006-1216|12/04/1996
Ryan Mercer|Long|(31) 44046-0956|15/08/2002
Nyssa Key|Ferguson|(41) 84094-3855|22/08/1980
Ginger Cook|Salas|(99) 67795-7229|28/07/1995
Ignatius Vega|Morrow|(51) 78061-0362|14/06/1962
Channing Hogan|Burks|(23) 98090-7542|22/01/1976
Hedley Wiggins|Bonner|(83) 67460-4139|08/08/1952
Matthew Payne|Harris|(49) 40988-7762|05/07/1961
Xandra Mills|Kelly|(59) 25283-7570|17/05/1996
Mia Reese|Cortez|(92) 34087-6128|17/12/1991
Mikayla Vega|Whitley|(68) 73497-9863|10/11/1999
Phillip Winters|Herring|(22) 71368-6144|06/11/1960
Libby Norton|Stephens|(52) 70105-9415|05/04/1962
Phelan Mcpherson|Vinson|(82) 61233-3541|03/10/1983
Amos Marsh|Bush|(89) 14916-5689|01/10/1959
Ayanna Patel|Dejesus|(26) 44504-4771|10/01/1979
Dean Acevedo|Watts|(66) 95065-4622|26/05/1980
Fiona Horn|Irwin|(94) 31750-7046|25/09/1978
Aubrey Coleman|Nieves|(56) 61184-4208|28/11/1965
Eliana Lowery|Graves|(15) 70740-3424|28/04/1969
Lucius Davidson|Monroe|(16) 55178-1237|10/05/1968
Hadley Tucker|Guerra|(61) 26655-5831|27/04/1977
Zeus Mcclain|Beasley|(83) 19787-1363|11/04/1981
Keith Pugh|Shepherd|(14) 75358-1168|26/07/1980
Rafael Francis|Trevino|(39) 81248-2292|27/12/2003
Hyatt Albert|Raymond|(58) 92564-2871|19/03/1990
Stuart Moody|Mclaughlin|(69) 27881-9427|01/11/1973
Bradley Montoya|Quinn|(39) 50213-6407|26/12/2001
Hoyt Vega|Robinson|(53) 77697-7500|10/04/1997
Jolene Keller|Mejia|(95) 57168-1953|28/05/1973
Colorado Christian|Stanley|(52) 38509-1426|22/12/1970
Dominique Stanley|Nielsen|(53) 42020-3407|15/10/1975
Joseph Carey|Whitehead|(96) 47656-2747|05/05/1987
Kadeem Noel|Morrison|(57) 39690-8719|28/01/1950
Chanda Richard|Shepard|(25) 75584-5664|06/09/1964
Jesse Wolf|Warner|(41) 81791-9720|25/12/1980
Nasim Vargas|Ferguson|(96) 29679-9628|17/09/1973
Camille Gordon|Conner|(10) 18887-4899|16/09/1950
Aspen Oneill|Blevins|(90) 79755-8639|24/07/1972
Levi Humphrey|Kirk|(52) 54129-0923|28/03/1959
Violet Acosta|Horton|(47) 66385-2162|05/05/1952
Liberty Beasley|Castillo|(12) 91849-7167|24/04/1997
Scott Baldwin|Harrell|(89) 87788-6892|04/04/1978
Dorothy Warner|Wise|(42) 11176-1814|13/03/1956
Mercedes Johnson|Guzman|(52) 83714-6144|05/06/1979
Buffy Wiggins|Sampson|(80) 37386-2328|18/10/1954
Jelani Nichols|Wood|(93) 74978-9263|13/02/1975
Ruby Burke|Powers|(48) 83729-1605|24/02/1989
Alec Joyner|Macdonald|(32) 52644-2394|29/01/1992
Alan Figueroa|Noble|(87) 54119-9097|29/06/2003
30 changes: 30 additions & 0 deletions LAB03/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Nome do executável
TARGET = main

# Compilador e flags
CXX = g++
CXXFLAGS = -Wall -Wextra -std=c++17 -pedantic-errors
LDFLAGS =

# Arquivos de código-fonte e objetos
SRCS = main.cpp Registro.cpp Buffer.cpp
OBJS = $(SRCS:.cpp=.o)

# Regras padrão
all: $(TARGET)

# Regra para criar o executável
$(TARGET): $(OBJS)
$(CXX) $(CXXFLAGS) -o $@ $^ $(LDFLAGS)

# Regra para criar objetos
%.o: %.cpp
$(CXX) $(CXXFLAGS) -c $< -o $@

# Limpeza
clean:
rm -f $(OBJS) $(TARGET)

# Execução
run: $(TARGET)
./$(TARGET)
38 changes: 38 additions & 0 deletions LAB03/Registro.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include "Registro.h"

std::string Registro::serializarDelimitadores() const {
std::ostringstream oss;
oss << nome << "|" << sobrenome << "|" << telefone << "|" << dataNascimento << "\n";
return oss.str();
}

std::string Registro::serializarDescritor() const {
std::ostringstream oss;
std::string dados = nome + "|" + sobrenome + "|" + telefone + "|" + dataNascimento;
size_t tamanho = dados.size();
oss.write(reinterpret_cast<const char*>(&tamanho), sizeof(size_t));
oss << dados;
return oss.str();
}

void Registro::desserializarDelimitadores(const std::string& linha) {
std::istringstream iss(linha);
std::getline(iss, nome, '|');
std::getline(iss, sobrenome, '|');
std::getline(iss, telefone, '|');
std::getline(iss, dataNascimento, '\n');
}

void Registro::desserializarDescritor(const std::string& buffer) {
std::istringstream iss(buffer);
size_t tamanho;
iss.read(reinterpret_cast<char*>(&tamanho), sizeof(size_t));
std::string dados(tamanho, '\0');
iss.read(&dados[0], tamanho);

std::istringstream dadosStream(dados);
std::getline(dadosStream, nome, '|');
std::getline(dadosStream, sobrenome, '|');
std::getline(dadosStream, telefone, '|');
std::getline(dadosStream, dataNascimento, '\n');
}
2 changes: 1 addition & 1 deletion LAB03/Registro.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ class Registro {
void desserializarDescritor(const std::string& buffer);
};

#endif
#endif
Binary file added LAB03/descricao.pdf
Binary file not shown.
71 changes: 71 additions & 0 deletions LAB03/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#include "Registro.h"
#include "Buffer.h"
#include <iostream>
#include <fstream>
#include <vector>

void carregarTXT(const std::string& nomeArquivo, std::vector<Registro>& registros) {
std::ifstream arquivo(nomeArquivo);
if (!arquivo.is_open()) {
throw std::runtime_error("Não foi possível abrir o arquivo de entrada: " + nomeArquivo);
}

std::string linha;
std::getline(arquivo, linha); // Ignorar cabeçalho
while (std::getline(arquivo, linha)) {
Registro reg;
reg.desserializarDelimitadores(linha);
registros.push_back(reg);
}
}

void salvarRegistrosBinario(const std::string& nomeArquivo, const std::vector<Registro>& registros) {
Buffer buffer(nomeArquivo, std::ios::out | std::ios::binary);
for (const auto& reg : registros) {
buffer.escreverRegistro(reg, true); // Com descritor
}
}

void salvarRegistrosDelimitadores(const std::string& nomeArquivo, const std::vector<Registro>& registros) {
std::ofstream arquivo(nomeArquivo);
if (!arquivo.is_open()) {
throw std::runtime_error("Não foi possível abrir o arquivo de saída: " + nomeArquivo);
}

arquivo << "Nome|Sobrenome|Telefone|Nascimento\n"; // Cabeçalho
for (const auto& reg : registros) {
arquivo << reg.serializarDelimitadores();
}
}

int main() {
try {
std::vector<Registro> registros;

// Carregar dados do arquivo TXT
carregarTXT("Dados.txt", registros);

// Salvar registros em formato binário
salvarRegistrosBinario("saida.bin", registros);

// Ler e exibir registros do binário
Buffer buffer("saida.bin", std::ios::in | std::ios::binary);
Registro reg;

std::cout << "Registros lidos do arquivo binário:" << std::endl;
while (buffer.lerRegistro(reg, true)) {
std::cout << "Nome: " << reg.nome << ", Sobrenome: " << reg.sobrenome
<< ", Telefone: " << reg.telefone
<< ", Data de Nascimento: " << reg.dataNascimento << std::endl;
}

// Salvar registros em formato delimitado
salvarRegistrosDelimitadores("saida_delimitadores.txt", registros);

} catch (const std::exception& ex) {
std::cerr << "Erro: " << ex.what() << std::endl;
return 1;
}

return 0;
}

0 comments on commit 23499c7

Please sign in to comment.