www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/08/20/14:19:45

From: i96csm AT river DOT tay DOT ac DOT uk
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Writing a struct to disk
Message-ID: <1997Aug20.130209.8@river.tay.ac.uk>
Date: 20 Aug 97 13:02:09 +0100
References: <33F93687 DOT 14CE173 AT psu DOT edu> <33F98A99 DOT 7AEC324C AT usa DOT net>
Organization: University of Abertay Dundee
Lines: 69
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

In article <33F98A99 DOT 7AEC324C AT usa DOT net>, Einstein <einstein DOT lam AT usa DOT net> 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        
  \   | /   |
   \___/    |
             

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019