对象未从文件中读取确切数据 [英] Object Not Read Exact Data From File
本文介绍了对象未从文件中读取确切数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想从文件中读取数据,但是当我将其写入文件时,它成功地写入了文件,但是当我从文件中删除时,我给了我垃圾值.请Plz找到我无法检测到它无法读取的错误?
以下是我在vc ++中的代码
I want to read data from file but when i write it on file it wrote on file successfully but when i rem from file i give me garbage value. Plz find the Bug That i could n''t detect how it not read?
The following is my code in vc++
// reading_from_file.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include<iostream>
#include<conio.h>
#include<fstream>
using namespace std;
struct node
{
int Id;
node*next;
node()
{
next=NULL;
}
};
struct node1
{
int id;
};
class LL
{
node*first;
public:
LL()
{
first=NULL;
}
void add(node*t)
{
if(first==NULL)
{
first=new node();
first->Id=t->Id;
first->next=NULL;
}
else
{
node*temp;
temp=first;
while(temp->next!=NULL)
temp=temp->next;
node*last=new node();
last->Id=t->Id;
last->next=0;
temp->next=last;
}
}
void show()
{
node*tfirst=first;
while(tfirst!=NULL)
{
cout<<"ID "<<tfirst->Id<<endl;
tfirst=tfirst->next;
}
}
};
int _tmain(int argc, _TCHAR* argv[])
{
node*insert=new node();
LL obj;
node1 ob;
fstream fs;
int i=1;
fs.seekg(0);
fs.open("Malik.txt",ios::in|ios::out|ios::binary|ios::app);
while(!fs.eof())
{
fs.read(reinterpret_cast<char*>(&ob),sizeof(node1));
cout<<"vALUE "<<(ob.id)<<endl;
insert->Id=ob.id;
obj.add(insert);
}
//while(i)
//{
// //cout<<"Enter Key ";
// for(;i<=10;i++)
// {//cin>>insert->Id;
// insert->Id=i*5;
// obj.add(insert);
// fs.write(reinterpret_cast<char*>(&obj),sizeof(LL));
// }//cout<<"Press 0 To Terminate";
// //cin>>i;
//}
fs.close();
//system("cls");
obj.show();
return 0;
}
推荐答案
没有看到您要写入文件的位置,我无法提出修复建议,但是我可以提出替代方案. 序列化 [ ^ ].这个想法是每个类/结构都负责保存/恢复自己的数据,这实际上简化了事情.
Without seeing where you are writing to the file, I can''t suggest a fix, however I can suggest an alternative. Serialization[^]. The idea is that each class/struct is responsible for saving/restoring its own data, which really simplifies things.
#include <stdio.h>
#include "stdafx.h"
#include<iostream>
#include<conio.h>
#include<fstream>
using namespace std;
struct node {
node *next;
int Id;
/*This is an initialiser list, which is only valid in a constructor
It has the same effect as "next = NULL; Id = id;", only it is a touch faster*/
node(int id) : next(NULL), Id(id) { } //Add a parameter in here for the value to make construction neater when you make it
void serialize(ostream &s) {
//recursively write to file
s.write((char *)&Id, sizeof(Id));
if (next != NULL) {
next->serialize(s);
}
}
static node *unserialize(istream &s) {
//recursively read from file
int i;
s.read((char *)&i, sizeof(i));
if (s.eof()) {
return NULL;
}
node *n = new node(i);
n->next = unserialize(s);
return n;
}
};
class LL {
node* first;
//This allows us to add directly to the end of the list, instead of iterating through each element.
//When you start getting a few hundred elements in the list you will notice the difference
node* last;
public:
LL() : first(NULL), last(NULL) { } //Another initialiser list
~LL() {
//Clean up the memory
node *n = first;
while (n != NULL) {
node *del = n;
n = n->next;
delete del;
}
}
void add(int i) {
if(first == NULL) {
first = new node(i);
last = first;
} else {
//Isn't this easier?
node *temp = new node(i);
last->next = temp;
last = temp;
}
}
void show() {
node *tfirst = first;
while (tfirst != NULL) {
cout<<"ID "<<tfirst->Id<<endl;
tfirst = tfirst->next;
}
}
void serialize(ostream &s) {
if (first != NULL) {
first->serialize(s);
}
}
static LL *unserialize(istream &s) {
LL *ll = new LL();
ll->first = node::unserialize(s);
ll->last = ll->first;
return ll;
}
};
int _tmain(int argc, _TCHAR* argv[]) {
LL *ll = new LL();
ll->add(1);
ll->add(2);
ll->add(4);
ll->add(3);
cout << "Original list:" << endl;
ll->show();
fstream fs;
//NOTE: This opens the file for appending.
//The original contents will remain in the file, and this will be added to the end
fs.open("Malik.txt", ios::out|ios::binary|ios::app);
ll->serialize(fs);
delete ll;
fs.close();
fs.open("Malik.txt", ios::in|ios::binary|ios::app);
cout << "Saved list:" << endl;
ll = LL::unserialize(fs);
ll->show();
delete ll;
fs.close();
return 0;
}
另外,您可以使用模板 [
Also, you could use Templates[^] for this, which would make it a little more dynamic, if you need lists of multiple data types.
这篇关于对象未从文件中读取确切数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文