STD: String file writing error --------- accidental

Source: Internet
Author: User

Write: (...);
File. Write (char *) & A, sizeof ());
Read: a * B = new ();
File. Read (char *) B, sizeof ());
// You must also use the class pointer (the class contains a string). Otherwise, the runtime error occurs (Memory Sharing Issues Related to STD: string)

The key is that no matter how big the string is, such as more than 100 characters, sizeof (a) does not change when the Read and Write functions are used, and it can still be written...

The heavy-load <,> operator is easy to implement, but it wastes a lot of space and is inefficient in searching ....

Well, I have found that when the string size is very large, even a few kilobytes of data will be okay, as long as it is read in the same program, there will be no problem...
And once it is hundreds of bytes, it will crash into two different programs for reading and writing separately ....

Bytes --------------------------------------------------------------------------------------

Sprite. h header file

# Ifndef _ sprite_h _ # DEFINE _ sprite_h _ # include <iostream> # include <string> class sprite {PRIVATE: STD: String compression Sion; // occupation STD: String weapon; // weapon static int count; // Number of public: SPRITE () {} sprite (STD: String partition Sion, STD: String weapon): partition Sion (partition sion ), weapon (weapon) {} void showsprite (); // display genie information}; int sprite: Count = 0; void sprite: showsprite () {++ count; STD:: cout <"Genie" <count <"Occupation:" <strong sion <"Weapon:" <weapon <STD: Endl;} # endif

Main. CPP main file # include <iostream> # include <fstream> # include <string> # include "Sprite. H "int main () {// create an object array sprite sprites [3] = {sprite (" Mage "," magic wand "), Sprite (" warrior ", "Maid maid maid maid bao Dao Zzzzz tu Long Bao Dao Z Zzzz maid maid maid knife knife maid knife zzzzz maid Maid maid knife knife maid knife maid maid maid maid Maid maid maid maid knife knife maid knife zzzzz maid Z maid maid maid knife knife maid knife zzzzz maid Knife maid maid maid maid maid bao Dao Zzzzz tu Long Bao Dao zzzzzzz tu Long Bao Dao Zzzzz ZZ maid maid maid knife knife maid knife zzzzz maid Knife knife maid knife maid maid maid maid bao Dao Zzzzz tu Long Bao Dao zzzzzzz tu Long Bao Dao Zzzzz tu Long Bao Dao ZZ Zzz maid maid maid knife knife maid knife zzzzz maid Dragon Sword maid knife knife maid knife maid maid maid maid Zzzz maid maid maid knife knife maid knife zzzzz maid Maid maid knife knife maid knife maid maid maid maid Maid maid maid maid knife knife maid knife zzzzz maid "), sprite ("Taoist", "Yi tianjian ")}; // Open the file STD: ofstream file ("file. dat", STD: IOS: ate | STD: IOS: Binary); If (! File) {STD: cout <"file opening failed! "; Abort (); // equivalent to exit} // write the file for (INT I = 0; I <3; I ++) file. write (char *) & sprites [I], sizeof (sprite); // close the file. close (); // create an object array sprite * rsprites [3]; // open the file STD: ifstream rfile ("file. dat ", STD: IOS: Binary); If (! Rfile) {STD: cout <"file opening failed! "; Return 1; // equivalent to exit} // read the file for (INT I = 0; I <3; I ++) {rsprites [I] = new sprite (); rfile. read (char *) rsprites [I], sizeof (sprite); rsprites [I]-> showsprite ();} // close the rfile file. close (); Return 0 ;}

Certificate -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Detach read/write

Main1.cpp

# Include <iostream> # include <fstream> # include <string> # include "Sprite. H "int main () {// create an object array sprite sprites [3] = {sprite (" Mage "," magic wand "), Sprite (" warrior ", "Maid maid maid maid bao Dao Zzzzz tu Long Bao Dao zzz ZZ maid maid maid "), sprite ("Taoist", "")}; // open the file STD: ofstream file ("file. dat ", STD: IOS: ate | STD: IOS: Binary); If (! File) {STD: cout <"file opening failed! "; Abort (); // equivalent to exit} // write the file for (INT I = 0; I <3; I ++) file. write (char *) & sprites [I], sizeof (sprite); // close the file. close (); // create an object array sprite * rsprites [3]; // open the file STD: ifstream rfile ("file. dat ", STD: IOS: Binary); If (! Rfile) {STD: cout <"file opening failed! "; Return 1; // equivalent to exit} // read the file for (INT I = 0; I <3; I ++) {rsprites [I] = new sprite (); rfile. read (char *) rsprites [I], sizeof (sprite); rsprites [I]-> showsprite ();} // close the rfile file. close (); Return 0 ;}

Main2.cpp

# Include <iostream> # include <fstream> # include <string> # include "Sprite. H "int main () {// create an object array sprite * rsprites [3]; // open the file STD: ifstream rfile (" file. dat ", STD: IOS: Binary); If (! Rfile) {STD: cout <"file opening failed! "; Return 1; // equivalent to exit} // read the file for (INT I = 0; I <3; I ++) {rsprites [I] = new sprite (); rfile. read (char *) rsprites [I], sizeof (sprite); rsprites [I]-> showsprite ();} // close the rfile file. close (); Return 0 ;}

There is no problem with reading and writing main1.cpp. If main2.cpp reads the file separately, it will be crash ..

The principle should be related to string ....

Summary:

In the class, it is best to use char STR [30]; these fixed lengths will be okay, and the written files will be fixed in size and will not cross-border or anything, and there will be no error in the analysis structure or anything...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.