TIP: Click on subject to list as thread! ANSI
echo: c_plusplus
to: JASON REYNOLDS
from: CLIFF RHODES
date: 1997-03-19 13:45:00
subject: Help with Classes

--> Jason Reynolds wrote to Cliff Rhodes <--
 
CR> // For array of chars
CR> void SetPlayerStr(const char *s) {
CR>     if(itsStr) delete [] itsStr;      // If used, free
CR>     itsStr = new char[strlen(s) + 1]; // Get new
CR>     strcpy(itsStr, s);  }             // Copy
JR>You were right in what I needed.  It's working excellent
JR>except one thing.  I'm having problems with it saving to a
JR>file. Is it not saving because it's an array of char's?
 
Jason, it's not saving because itsStr is a pointer. You are just saving
the pointer and not the char's it points to. The data gets lost when the 
object goes away--then the pointer is meaningless.
 
Someone else suggested that you try fixed size arrays instead of the
method I gave. Perhaps this would work better for you--provided you know
ahead of time the maximum array size. Here's an example
#include 
#include 
 
const int MaxStrLen = 36;   // Maximum string length for PlayerInfo
class PlayerInfo {
  public:
    PlayerInfo() { PlayerStr[0] = '\0'; }  // Initially empty str
 
    void SetPlayerStr(const char *s) {
              strncpy(PlayerStr, s, MaxStrLen - 1);
              PlayerStr[MaxStrLen - 1] = '\0';  }
 
    char *GetPlayerStr(char *s) const {
              return strcpy(s, PlayerStr); }
    // Test if object p == '*this' by comparing strings in 
    // overloaded == operator.
    bool operator == (PlayerInfo & p) const {
         return strcmp(PlayerStr, p.PlayerStr) == 0; }
    // Overload the << operator for this class. Forces write of
    // entire string to ostream.
    friend ostream & operator << (ostream & os, PlayerInfo & p) {
        return os.write(p.PlayerStr, MaxStrLen); }
 
    // Overload the >> operator for this class. Forces read of
    // entire MaxStrLen bytes from the stream.
    friend istream & operator >> (istream & is, PlayerInfo & p) {
        is.read(p.PlayerStr, MaxStrLen);
        p.PlayerStr[MaxStrLen - 1] = '\0';
        return is; }
 
  private:
    char PlayerStr[MaxStrLen];  // All that trouble for this?!
};
 
#include 
#include    // Needed for remove() prototype
 
int main(void)
{
  PlayerInfo p, q;
 
  p.SetPlayerStr("Jason Reynolds");
  char s1[MaxStrLen];
  cout << "Player name: " << p.GetPlayerStr(s1) << endl;
  // Note, you could get the same thing with
  // p.GetPlayerStr(s1); cout << "Player name: " << s1 << endl;
  // Open file "temp" for writing...
  ofstream outFile("temp", ios::binary);
  if(outFile)
  {
    outFile << p;    // Use overloaded << operator to save the string
    outFile.close();
  }
  else
  {
    cout << "ERROR: couldn't create file for writing" << endl;
    return 1;
  }
  // Open file "temp" for reading...
  ifstream inFile("temp", ios::binary);
  if(inFile)
  {
    inFile >> q;    // Use overloaded >> operator to get the string
    inFile.close();
  }
  else
  {
    cout << "ERROR: unable to open file for reading" << endl;
    remove("temp");
    return 1;
  }
  if(p == q)        // Use overloaded == to see if the strings are equal
    cout << "Success, read and write data correct!" << endl;
  else
    cout << "ERROR: strings didn't match after file operations" << endl;
 
  remove("temp");   // Delete the file temp
 
  return 0;
}
To handle other class members, you will need to make additions to the 
==, > overloaded operators. For instance, it there is an
int PlayerScore; member, the << operator might become:
friend ostream & operator << (ostream & os, PlayerInfo & p) {
    os << PlayerScore;  // Just output the int value
    return os.write(p.PlayerStr, MaxStrLen); }
 
X CMPQwk 1.42 1692 X"Manners are the hypocrisy of a nation." - Honore De
--- Maximus/2 3.01
---------------
* Origin: COMM Port OS/2 juge.com 204.89.247.1 (281) 980-9671 (1:106/2000)

SOURCE: echomail via exec-pc

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.