gpt4 book ai didi

c++ - 从二进制文件中检索数据,无意义的字符

转载 作者:行者123 更新时间:2023-11-28 07:46:18 26 4
gpt4 key购买 nike

我正在尝试从二进制文件中检索一些数据以将它们放入链表中,这是我写入文件的代码:

void Pila::memorizzafile()
{
int contatore = 0;
puntarec temp = puntatesta;
ofstream miofile;
miofile.open("data.dat" , ios::binary | ios::out);
if(!miofile) cerr << "errore";
else
{
while(temp)
{
temp->elem.writes(miofile);
contatore++;
temp = temp->next;
}
//I go back at the beginning of the file to write how many elements I have
miofile.seekp(0, ios::beg);
miofile.write((const char *)&contatore , sizeof(int));
miofile.close();
}
}

函数中写道:

void Fiche::writes(ofstream &miofile)
{
//Valore.
miofile.write((const char *)&Valore,sizeof(int));
//Materiale, I write the dimension of the string.
int buff = strlen(Materiale);
miofile.write((const char *)&buff,sizeof(int));
//Writing the string
miofile.write(Materiale,buff*sizeof(char));
//Dimension of Forma
buff = strlen(Forma);
miofile.write((const char*)&buff,sizeof(int));
//The string itself
miofile.write(Forma,buff*sizeof(char));
//Dimension of Colore.
buff = strlen(Colore);
miofile.write((const char*)&buff,sizeof(int));
//The string
miofile.write(Colore,buff*sizeof(char));
}

现在对于阅读部分,我正在尝试制作一个应该能够直接从文件中读取的构造函数,这里是:

Pila::Pila(char * nomefile)
{
puntatesta = 0;
int contatore = 0;
ifstream miofile;
miofile.open(nomefile , ios::binary | ios::in);
if(!miofile) cerr << "errore";
else
{
//I read how many records are stored in the file
miofile.read((char*)&contatore,sizeof(int));
Fish temp;
for(int i = 0; i < contatore; i++)
{
temp.reads(miofile);
push(temp);
}
miofile.close();
}
}

还有阅读功能:

void Fiche::reads(ifstream &miofile)
{
//I read the Valore
miofile.read((char*)&Valore,sizeof(int));
//I create a temporary char *

char * buffer;
int dim = 0;
//I read how long will be the string
miofile.read((char*)&dim,sizeof(int));
buffer = new char[dim];
miofile.read(buffer,dim);
//I use the set function I created to copy the buffer to the actual member char*
setMateriale(buffer);
delete [] buffer;
//Now it pretty much repeats itself for the other stuff
miofile.read((char*)&dim,sizeof(int));
buffer = new char[dim];
miofile.read(buffer,dim);
setForma(buffer);
delete [] buffer;
//And again.
miofile.read((char*)&dim,sizeof(int));
buffer = new char[dim];
miofile.read(buffer,dim);
setColore(buffer);
delete [] buffer;
}

代码没有给我任何错误,但在屏幕上我读取了随机字符,甚至与我在文件中写的内容相去甚远。有谁能帮帮我吗?

编辑:

这里有一个输入和输出的例子:

Fiche A("boh" , 4 , "no" , "gaia");
Fiche B("Marasco" , 3 , "boh" , "nonnt");
Fiche C("Valori" , 6 , "asd" , "hey");
Fiche D("TipO" , 7 , "lol" , "nonloso");
Pila pila;
pila.push(A);
pila.push(B);
pila.push(C);
pila.push(D);
pila.stampa();
pila.memorizzafile();

和:

Pila pila("data.dat");
pila.stampa();

最佳答案

这可能是你的错误:

            //I go back at the beginning of the file to write how many elements I have
miofile.seekp(0, ios::beg);
miofile.write((const char *)&contatore , sizeof(int));
miofile.close();

通过寻找开始,然后写作。您正在覆盖第一个对象的一部分。

我认为最好的办法是先遍历列表并计算元素。写这个然后继续写所有的元素。无论如何它可能会更快(但您可以确定时间)。

我认为您正在使用许多 C 结构的方式来保存东西。

此外,除非您要保存大量信息,否则我建议不要使用二进制格式。文本格式(用于您的数据)可能同样好,并且是人类可读的,因此您可以查看文件并找出问题所在。

关于c++ - 从二进制文件中检索数据,无意义的字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14815837/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com