Note:
I wrote a code based on what you said, that I wanted the code to do. to better demonstrate the response in terms of C functionalities++.
Details:
You said you’re programming in C++ but is using a style similar to C not that it’s wrong, but, for example, there’s no need to use Naked pointers to manipulate the struct you created, or pass a const char* to the reading function, but that’s not the case with the question, what I wanted to tell you with this is for you to study the "features" that language offers you, rather than trying to do the old-fashioned way.
Answer:
I believe that in your code what is wrong in the reading function, is the use of read the way you used,
input.read((char*)(&vector_records[i]), sizeof(record));
And also after the loop is in which you display the data, you are setting vector_recordsas NULL before deleting it:
vector_records = NULL;
delete[] vector_records;
where the right one would be:
delete [] vector_records;
vector_records = nullptr;
Code:
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
struct banda_rock
{
std::string nome;
int numero_integrantes;
std::string estilo;
int ano_criacao;
std::string musica_famosa;
};
bool write( const std::string& file_name, const std::vector<banda_rock>& v_registers );
std::vector< banda_rock > read( const std::string& file_name );
void insert( const size_t& num_registers, std::vector<banda_rock>& v_registers );
int main()
{
//quant deve ser size_t por que já que você tem a intenção de expressar a
//quantidade de algo, e essa quantidade não pode ser negativa
//(size_t só aceita valores positivos e o 0( no seu caso você não precisa do 0) )
size_t quant = 0;
std::string file_name;
std::vector< banda_rock > v_catalogo;
//Caso o que a pessoa digite não seja um número ou um numero menor que zero
if( quant == 0 )
{
std::cout << "\nDigite a quantidade de bandas : ";
std::cin >> quant;
while( std::cin.fail() )
{
std::cin.clear( std::ios::goodbit );
std::cin.ignore();
std::cin >> quant;
}
}
if( quant > 0 )
{
std::cout << "Digite o nome do arquivo a ser armazenado as bandas (com a extensao) : ";
std::cin >> file_name;
while( std::cin.fail() )
{
std::cin.clear( std::ios::goodbit );
std::cin.ignore();
std::cin >> file_name;
}
}
//chama a função que vai popular o vetor
insert( quant, v_catalogo );
//escreve arquivo
if( write( file_name, v_catalogo ) )
{
//se a operação de escrita obteve sucesso
v_catalogo.clear();
v_catalogo.shrink_to_fit();
v_catalogo = read( file_name );
}
else{ std::cout << "\nNão foi possivel escrever no arquivo." << std::endl; }
for ( size_t i = 0; i < v_catalogo.size(); i++)
{
std::cout << "\tNome: " << v_catalogo[ i ].nome << '\n'
<< "\tIntegrantes: " << v_catalogo[ i ].numero_integrantes << '\n'
<< "\tEstilo: " << v_catalogo[ i ].estilo << '\n'
<< "\tAno de criação: " << v_catalogo[ i ].ano_criacao << '\n'
<< "\tMúsica Famosa: " << v_catalogo[ i ].musica_famosa << "\n\n";
}
return 0;
}
bool write( const std::string& file_name, const std::vector<banda_rock>& v_registers )
{
if( v_registers.empty() ){ return false; }
std::ofstream out;
out.open( file_name, std::ios::binary|std::ios::app );
if( !out.is_open() ){ out.close(); return false; }
else
{
for ( size_t i = 0; i < v_registers.size(); i++)
{
//o # pode servir para posteriormente para você contar quantos
//registros você têm (fica a seu critério usá-lo ou não)
out << '#' << ' '
<< v_registers[ i ].nome << ' '
<< v_registers[ i ].numero_integrantes << ' '
<< v_registers[ i ].estilo << ' '
<< v_registers[ i ].ano_criacao << ' '
<< v_registers[ i ].musica_famosa << '\n';
}
}
out.close();
return true;
}
std::vector< banda_rock > read( const std::string& file_name )
{
std::ifstream in;
size_t quant = 0;
char ch_aux;
in.open(file_name, std::ios::binary);
if ( !in.is_open() ){ in.close(); }
else
{
while( !in.eof() )
{
in.get( ch_aux );
if( ch_aux == '#' ){ quant++; }
}
}
in.close();
std::vector<banda_rock> v_registers;
v_registers.resize( quant );
in.open(file_name, std::ios::binary);
if ( !in.is_open() ){ in.close(); }
else
{
for( size_t i = 0; i < v_registers.size(); i++ )
{
in.ignore( 1, '#' );
in >> v_registers[ i ].nome
>> v_registers[ i ].numero_integrantes
>> v_registers[ i ].estilo
>> v_registers[ i ].ano_criacao
>> v_registers[ i ].musica_famosa;
}
}
in.close();
return v_registers;
}
void insert( const size_t& num_registers, std::vector<banda_rock>& v_registers )
{
v_registers.resize( num_registers );
std::cout << "insira os dados da banda:" << std::endl;
for( size_t i = 0; i < v_registers.size(); i++ )
{
std::cout << " Nome da banda [" << i + 1 << "]: ";
std::cin >> v_registers[ i ].nome;
while( std::cin.fail() )
{
std::cout << "\tpor favor digite o nome da banda [" << i + 1 << "]." << std::endl;
std::cin.clear( std::ios::goodbit );
std::cin.ignore();
std::cin >> v_registers[ i ].nome;
}
std::cout << " Número de integrantes da banda [" << i + 1 << "]: ";
std::cin >> v_registers[ i ].numero_integrantes;
while( std::cin.fail() )
{
std::cout << "\tpor favor digite o número de integrantes da banda [" << i + 1 << "]." << std::endl;
std::cin.clear( std::ios::goodbit );
std::cin.ignore();
std::cin >> v_registers[ i ].numero_integrantes;
}
std::cout << " Estilo da banda [" << i + 1 << "]: ";
std::cin >> v_registers[ i ].estilo;
while( std::cin.fail() )
{
std::cout << "\tpor favor digite o estilo da banda [" << i + 1 << "].";
std::cin.clear( std::ios::goodbit );
std::cin.ignore();
std::cin >> v_registers[ i ].estilo;
}
std::cout << " Ano de criação da banda [" << i + 1 << "]: ";
std::cin >> v_registers[ i ].ano_criacao;
while( std::cin.fail() )
{
std::cout << "\tpor favor digite o ano de criação da banda [" << i + 1 << "].";
std::cin.clear( std::ios::goodbit );
std::cin.ignore();
std::cin >> v_registers[ i ].ano_criacao;
}
std::cout << " Musica Conhecida da banda[" << i + 1 << "]: ";
std::cin >> v_registers[ i ].musica_famosa;
while( std::cin.fail() )
{
std::cout << "\tpor favor digite uma música conhecida da banda [" << i + 1 << "].";
std::cin.clear( std::ios::goodbit );
std::cin.ignore();
std::cin >> v_registers[ i ].musica_famosa;
}
}
}
Gives error in reading and runs the Cout "Error in reading file"? It may have nothing to do with it but have you ever thought about changing if(input) to if(input.good()) ? I usually test this way.
– PerryWerneck
It does not even display the Cout "Error in reading the file". The execution of the program ends before, it shows the text "Band name: " and then gives the error. I will test with this condition in the if you spoke. Thanks!
– Murilo Augusto Teixeira Biagi
Also put the structure/class definition
registro
as well as an example of the file you are trying to read, so it is easy to reproduce the problem.– Isac
Now that I noticed! Are you reading the block directly in the structure, alias, is it a structure or a class? How is its definition? I already say that if the field "name" is string it is certain that will give problem. (-:
– PerryWerneck
That is, it’s a real structure. The "name" field is a string and has two other fields in the structure that are string. I don’t understand why it is a problem if it is xD string. I will post the structure definition and the code you write to the file.
– Murilo Augusto Teixeira Biagi
I edited the question by asking what you asked for. If you could take a look, I would really appreciate it.
– Murilo Augusto Teixeira Biagi
@Muriloaugustoteixeirabiagi in writing you are converting from a direct struct to a char pointer, probably the file is already recording corrupted.
– prmottajr
The problem is that the write function takes a char* as the first argument, so I need to cast a char* of my struct at the time of recording.
– Murilo Augusto Teixeira Biagi