Why wont this Class File I/O work

This is a discussion on Why wont this Class File I/O work within the C++ Programming forums, part of the General Programming Boards category; Ive uploaded a file, it has the class member functions on it from my project, ListOfObjects::save() is the one giving ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    34

    Why wont this Class File I/O work

    Ive uploaded a file, it has the class member functions on it from my project, ListOfObjects::save() is the one giving me the trouble, for some reason it wont let me save the contents of an array of a type object which itself is made up of two different object types. If you have a look you will see what i mean, maybe im coming at it from the wrong angle but i cant seem to get it right.

    Any help will be much appreciated.

    Thanks

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    34
    Here it is
    Attached Files Attached Files

  3. #3
    Registered User biosx's Avatar
    Join Date
    Aug 2001
    Posts
    230
    Code:
    int ListOfObjects::save( ostream & out )
    {
    	ofstream ObjectsFile( "Objects.dat", ios::out);
    
    	int Index = 0;
    
    	if( !ObjectsFile )
    	{
    		cerr << "File could not be opened" << endl;
    
    	}
    
    	for( Index = 0; Index < cCount; Index++ )
    		{
    			ObjectsFile << cList[Index];
    		}
    	return 0;
    }
    You have a functin parametr that you never use. Plus I would do it another way. This is how I would implement a saving function:
    Code:
    #include <string.h>     // strcpy()
    #include <fstream.h>  // file i/o
    
    class Stuff
    {
       public:
          Stuff();
          int save(Stuff *sPtr);
          char string[10];
          char stringTwo[10];
          int num;
    
    };
    
    /* Constructor */
    Stuff::Stuff()
    {
       for(int i = 0; i < 10; i++)
       {
          string[i] = 0;
          stringTwo[i] = 0;
       }
       num = 0;
    }
    
    /* Save Member Function */
    int Stuff::save(Stuff *sPtr)
    {
       ofstream outfile("Outfile.txt");
    
       if( !outfile )
       {
          cerr << "Couldn't open file" << endl;
          return 1;
       }
    
       outfile << sPtr->string << endl
                  << sPtr->stringTwo << endl
                  << sPtr->num << endl;
    
       outfile.close();  // I don't trust destructors
    
       return 0;
    }
    
    int main()
    {
       Stuff stuffObject;
    
       strcpy(stuffObject.string, "Hello");
       strcpy(stuffObject.stringTwo, "Yo");
       stuffObject.num = 10;
    
       stuffObject.save(&stuffObject);
    
       return 0;
    }
    Hope that helps.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    34
    Thanks alot m8, ill let you know how i get on.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. #include header files or .cpp files?
    By DoctorX in forum C++ Programming
    Replies: 3
    Last Post: 12-23-2006, 11:21 AM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 10:03 PM
  4. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  5. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21