From: Einstein Newsgroups: comp.os.msdos.djgpp Subject: Re: Writing a struct to disk Date: Thu, 21 Aug 1997 08:09:53 +0800 Organization: n/a Lines: 95 Message-ID: <33FB8751.A3FD1146@usa.net> References: <33F93687 DOT 14CE173 AT psu DOT edu> <33F98A99 DOT 7AEC324C AT usa DOT net> <1997Aug20 DOT 130209 DOT 8 AT river DOT tay DOT ac DOT uk> NNTP-Posting-Host: ip-49-139.dialup.hkstar.com Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Precedence: bulk > slight problem. > You need to add the function > > struct node { > int number; > unsigned char age; > char *name; > node *next; > > friend ostream & operator(ostream &,const node&); > } Thank you for telling me the slight problem. You have prevented me from hours of frustration and debugging. However, I am sure that your code cannot pass the compiler, too. friend ostream & operator(ostream &,const node&); How come there is not any punctuation between the operator keyword and the left parenthesis? Einstein einstein DOT lam AT usa DOT net i96csm AT river DOT tay DOT ac DOT uk wrote: > > In article <33F98A99 DOT 7AEC324C AT usa DOT net>, Einstein writes: > > struct node { > > int number; > > unsigned char age; > > char *name; > > node *next; > > } > > > > If you want to save this struct, write this operator: > > ostream& operator <<( node src ); > > > > Assumptions: > > You are using C++. > > You can get the string length by some means. For instances, size_t strlen( > > const char *string ); > > > > Remarks: > > If the variable length string is allocated using the new operator, do not > > use the old char* and allocate the memory every time. > > Remember to save the string length. > > > > Einstein > > einstein DOT lam AT usa DOT net > > > > > > > > > > slight problem. > You need to add the function > > struct node { > int number; > unsigned char age; > char *name; > node *next; > > friend ostream & operator(ostream &,const node&); > } > > and define it as > > ostream & operator(ostream & out,const node & n) > { > out << n.number << '\n' << (int) n.age << '\n' > << n.age << 'n' > > if (n.next == NULL) > out << "NULL\n"; > else > out << (n.next)->number << '\n'; > > return out; > } > > But this is C++, and doesn't help if you are using C > -- > ___ . > / /|\ /| > / / | \ / | Colin S. Miller > / \ | v | csmiller AT iname DOT com > | \| | > | \ | PgDip/MSc Software Engineering > | |\ | University of Abertay > \ | \ | (01382) 308000 ext 2800 > \ | / | > \___/ | >