Thread: Trouble writing to file

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    52

    Trouble writing to file

    First off, this is for a class, I'm really trying to learn this and be able to do it on my own but when a hang up occurs and you only have 24 hours to finish it one must take drastic actions. So I plead for help from the learned ones...

    I want to write the information stored in the 'Pets' array to a file called 'animals.txt...

    I know that this code looks really twisted, I'm using a list to automatically populate my data members of the 'Animal' class.

    Where am I going wrong?

    Code:
    //initial code, initialization of data members....	
    
    
    fstream theFile("C:\\Junkdata\\Animals.txt");
    	
    	//fstream theFile((char *) &Pets, ios::out | ios::app | ios::binary);
    	if(!theFile)
    	{
    		cout << "File object not created...";
    		return 999;
    	}
    
    	
    
    	theFile.close();
    
    	theFile.open((char *) &Pets, ios::in | ios::binary);
    	if(theFile.fail())
    	{
    		cout << "Unable to open input file....";
    		return 9999;
    	}
    
    	//Animal generic;
    	
    
    	theFile.read((char *) &Pets, sizeof(Animal)); 
    	for(int a(0); a < count; a++)
    	{
    		theFile.write((char *) &Pets[a++], sizeof(Animal));
    		if(a = 0)
    		{
    			Header();
    		}
    		if(a == count)
    		{
    			cout << "Total number of animals: " << count << endl;
    			cout << endl << "David Evans" << endl;
    			cout << "ITSE-2431.005" << endl;
    			cout << "May 2nd, 2002" << endl;
    		}
    	}
    	
    	
    	ofstream outFile("C:\\JunkData\\Animals.txt");
    	
    	if(!outFile)
    		{
    		cout << "Unable to open file...";
    			return 999;
    		}
    
    	
    
    	return 0;
    }
    I've excluded the 'Animal' class as I am not having any difficulty with it and did not think it was relevant for the problem that I'm having. Should anybody like to view it I'll post it immediately.

    Thanks a lot for any assistance, hints, and clues.
    Last edited by Fyodorox; 05-06-2002 at 05:18 PM.
    MS VC++ 6.0

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trouble Writing To File
    By NuNn in forum C Programming
    Replies: 2
    Last Post: 03-23-2009, 09:44 AM
  2. Trouble writing characters to a file.
    By themusician31 in forum C Programming
    Replies: 7
    Last Post: 08-10-2008, 05:50 AM
  3. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  4. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM