当我在结构中使用STL字符串var时,调试断言失败 [英] Debug Assertion Failed when I use a STL string var in a struct
问题描述
当我读/写具有数据结构的二元文件时,会出现此错误(它具有字符串成员)
这是我的代码:
I have this error when I read/write binanry file with data are structs (it has string members)
this is my code:
//read data from file to linked list
int ReadFile(char* fname, Node* &head)
{
Node* cur;
BOOK a;
ifstream iFile;
iFile.open(fname, ios::binary);
if(iFile.good() == 0)
return 0;
while( iFile.read((char*)&a, sizeof(a)) )
{
if (head==NULL)
{
head = new Node;
head->data = a;
head->pNext = NULL;
cur = head;
}
else
{
cur->pNext = new Node;
cur = cur->pNext;
cur->data = a;
cur->pNext = NULL;
}
}
iFile.close();
return 1;
}
//write data from LL to file
int WriteFile(char* fname, Node* head)
{
Node* cur = head;
ofstream oFile;
oFile.open(fname, ios::binary);
if(oFile.good() == 0)
return 0;
while(cur != NULL)
{
WriteNode(oFile, cur->data);
cur = cur->pNext;
}
oFile.close();
return 1;
}
//write one node
void WriteNode(ofstream& out, BOOK a)
{
out.write((char*)&a, sizeof(a));
}
当我创建了data.bin文件来保存信息时.我在main()函数中调用ReadFile函数来读取数据.我得到了错误.
http://diendan.congdongcviet.com/attachment.php?attachmentid=8807&d=1331693026 [ ^ ]
这是我的结构:
when I have created data.bin file which save the info. I call ReadFile function in main() function to read data. I got the error.
http://diendan.congdongcviet.com/attachment.php?attachmentid=8807&d=1331693026[^]
this is my struct:
struct BOOK
{
string isbn;
string bookname;
string genre;
string author;
string publisher;
int year;
int amount;
};
struct Node
{
BOOK data;
Node* pNext;
};
那么如何解决这个问题呢?
谢谢!!
p/s:如何插入图像?
So how to solve this problem?
thank you!!
p/s: how to insert an image?
推荐答案
1.将Book *
强制转换为char *
是一件非常非常糟糕的事情.别.因此,按值将Book传递给函数WriteNode
.许多复制构造函数在此处运行.
2.您不必插入图像:如果在打开消息框时按CTRL-C,则消息框中的文本将被复制到剪贴板.
最好的祝福,
Pablo.
1. Casting aBook *
to achar *
is a very, very bad thing. Don''t. So is passing a Book by value to functionWriteNode
. Lots of copy constructors running there.
2. You don''t have to insert an image: if you press CTRL-C when you have a message box open, the text in the message box gets copied to the clipboard.
Best wishes,
Pablo.
您应正确序列化 ["STL类的序列化" [
You should properly serialize[^] your struct
. See, for instance: "Serialization of STL class"[^].
车轮,见面您的新发明家.开火,认识你的新发现者.
我知道您可能已经由一位讲师强加给您的这个烦人的小问题,他认为您必须从第一原理中学到如何编程C ++.但是,如果您使用的是字符串,那么为什么不花大量精力去使用标准库中的列表呢?
定义书籍后,创建清单非常简单:
std::list<book> my_books;
到此为止,所有被[1]丢弃的节点都放屁了.接下来是如何从文件中读取书籍并将其写入其中.读写只是复制,所以为什么不使用标准库中的复制算法呢?
编写它们:
Wheel, meet your new inventor. Fire, meet your new discoverer.
I know you''ve probably had this annoying little problem foisted on you by a lecturer that thinks you have to learn how to program C++ from first principles. However you''re using strings so why not go the whole hog and use a list from the standard library?
Once you''ve defined your books then creating a list is pretty simple:
std::list<book> my_books;
There you go, all the farting about with nodes disposed of [1]. Next up is how you read your books from a file and write them to it. Reading and writing is just copying so why not use the copy algorithm from the standard library?
To write them:
std::copy( my_books.begin(),
my_books.end(),
std::ostream_iterator<book>( file_stream ) );
阅读它们:
To read them:
std::copy( std::istream_iterator<book>( file_stream ),
std::istream_iterator<book>(),
std::back_inserter( my_books ) );
现在这里的草丛中有一条蛇...是的,您必须编写一些代码.您要做的就是为您的结构Book实现插入和提取运算符(<<>>).这样,您所有的代码都将像梦想一样工作.或一场噩梦:-).而且您将能够在不更改代码的情况下从控制台读取和写入书籍.
最后一件事-如果您可以聘请一位更好的C ++老师,那就去选一个.上个世纪以来,实施自己的清单是如此,任何让您自己编写清单的人都是施虐者.
干杯,
灰
[1]您现在可以开玩笑了:
编码器:我的代码没有节点"
评论者:闻起来如何?"
编码器:可怕!"
如果您不是讲英语的人并且错过了这个玩笑,请不要担心,除非您听过原著,甚至还算是有点可怜,否则它不是那么有趣.
Now there''s a snake in the grass here... Yep, you have to write some code. What you have to do is implement insertion and extraction operators (<< and >>) for your structure, Book. Then you all that code will work like a dream. Or a nightmare :-). And you''ll have the bonus of being able to read and write Books from the console with no code changes.
One final thing - if you can get a better C++ teacher go and get one. Implementing your own lists is so last century and anyone that''s making you write your own is a sadist.
Cheers,
Ash
[1] You can now make the joke:
Coder: "My code has no nodes"
Reviewer: "How does it smell?"
Coder: "Terrible!"
If you''re not a native English speaker and miss the joke, don''t worry, it ain''t that funny unless you''ve heard the original and even then it''s a bit poor.
这篇关于当我在结构中使用STL字符串var时,调试断言失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!