C + + writes the data to the linked list, writes the linked list to the file, and then reads the contents of the file __c++

Source: Internet
Author: User
Even if the world is deserted, there is always a person, he will be your believer. ----"The stars in the canoe" First step: Create a nodeTemplate < typename T > class Node {public:node (t data) {m_data = data;     M_pnext = NULL; }
Const T & GetData () {return m_data; }
Node < T > * & GetNext () {return m_pnext;     } private:t m_data; Node * M_PNEXT;      }; The class template is used here because the data part type here cannot be determined.

Step Two: Create a linked listTemplate < typename T > class List {public:list () {m_ilen = 0;     M_pfirstnode = NULL; }
Node < T >* Getfirstnode () {return m_pfirstnode; }
int Getlistlen () {return m_ilen; }
void Insert (t data) {Node < T > * node = new Node < T > (data);         Node-> getNext () = M_pfirstnode;         M_pfirstnode = node;     M_ilen + +; }
void display () {Node < T > * tmp = m_pfirstnode;             for (int i = 0; i < M_ilen i + +) {cout << tmp-> getData () << Endl;         TMP = tmp-> getNext (); }     }
Private:int M_ilen; Node < T > * M_PFIRSTNODE;        }; It is customary to define a list header that is different from other nodes, and a list header can put a lot of information, such as the length of the list, the author, the modifier, the date of creation, and so on, and not every node will have this information when the latter node joins.

... You can try the list in the main function ... int main (void) {list<int> intlist;     Intlist.insert (2);     Intlist.insert (1);     Intlist.insert (5);     Intlist.insert (3);     Intlist.insert (4);     cout << "Print List" << Endl; Intlist.display ();

Fourth Step: When the list is written, we write the linked list to the fileFStream file1;         File1.open ("Int.txt");         File1 << intlist; Open the file as read and write, remember to create the file Int.txt in advance, because when you open the file in read and write mode, if the file does not exist, the program will not automatically create. Similarly, it is the same with read-only opening, which is automatically created only when it is opened with write-only. If this is the only way to write, it will be compiled Unable to passBecause the data for a linked list type cannot be written directly to the file. We need to << operator overloading. overloaded operators: <<Template<typename t> fstream &operator << (fstream &file, list<t> &list) {Node<T>     *tmp = List.getfirstnode ();     int len = List.getlistlen ();         for (int i = 0; i < len; i++) {file << tmp->getdata ();     TMP = Tmp->getnext (); } return file; After writing, then executes file1.intlist; There won't be an error.

Fifth step: Read data from the file to the listFILE1.SEEKG (0, Ios_base::beg);     cout << "--------read the list in the file------------" << Endl;     List<int> IntList2;     int data = 0;         while (1) {file1 >> data;         Intlist2.insert (data);         if (file1.eof ()) {break;     } intlist2.display (); File1.close (); The idea is very simple, first set the file read and write location to the header, because we write the file, read and write location to the end, this time the direct reading is not read data. Then read the contents of the file into the temporary variable data, and then insert the call with the parameter constructor, input parameters data, generate a new list. After the exit is read, the final print is displayed.


The list above is valid only for general data types, if our data is part of our own constructed class. First step: Create a Class (take student as an example)Class Student {public:student (int id = 1000, string name = "", Float score = 0.0f) {m_iid = ID;         M_strname = name;     M_fscore = score; }
Friend Ostream & operator << (Ostream & Out, const Student & Stu);
Private:int m_iid;      String M_strname; float M_fscore; };

Step Two: overloaded operators <<Ostream & operator << (Ostream & Out, const Student & Stu) {out << "" << Stu. M_iid << "" << Stu. M_strname << "" << Stu. M_fscore; Here the overload is because 1, when printing can not directly enter a student type, that is: cout << Stu is not directly implemented, 2, write the file can not write directly to a student type. But why only overload the << in Ostream can achieve both at the same time. Because FStream inherited iostream, and iostream inherited IStream and Ostream. The relationship is as follows:

After this the operation is very similar, we can understand by ourselves. Here is the full code, and the print result:
#include <iostream> #include <fstream> using namespace std;
Class Student {public:student (int id = 1000, string name = "", Float score = 0.0f) {m_iid = ID;         M_strname = name;     M_fscore = score; }
Friend Ostream & operator << (Ostream & Out, const Student & Stu); Friend FStream &operator << (fstream &file, const Student &stu);     Private:int m_iid;      String M_strname; float M_fscore; };
Ostream & operator << (Ostream & Out, const Student & Stu) {out << "" << Stu. M_iid << "" << Stu. M_strname << "" << Stu. M_fscore; }
Template < typename T > class Node {public:node (t data) {m_data = data;     M_pnext = NULL; }
     Const T & GetData

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.