Thread: Trouble writing to file

  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

  2. #2
    eat my shorts!
    Join Date
    Apr 2002
    Posts
    294
    omg u dont need to post the whole thing u know,
    try to get the exact or some wat close position of the error

  3. #3
    Registered User
    Join Date
    May 2002
    Posts
    317
    It appears that your creating a txt file for input/output and then reading/writing to it in binary format. Was this intentional ( I don't really even know if that works, honestly) also in this:
    Code:
    for(int a(0); a < count; a++)
    	{
    		theFile.write((char *) &Pets[a++], sizeof(Animal));
    		if(a = 0)// you're setting a to zero
    		{
    			Header();
    		}
    //etc..
    Now if you just want to write/read to/from a text file:
    Code:
    #include <fstream.h>
    #include <iostream.h>
    
    void main()
    {
           fstream SomeFile;
           SomeFile.open("your file", ios :: out|ois::in);
           SomeFile << "whatever to write";
           SomeFile >> "whatever to read";
           SomeFile.close();
    }
    Last edited by Traveller; 05-06-2002 at 04:52 PM.

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    52
    DayKnight,
    I know that beggars can not be choosers, but if you have to offer than a critique on length, save it.I am sorry if you're troubled by reading a long bit of syntax, I know it's confusing to be thorough is better than to leave out some snippet of code that would leave somebody guessing. I'll try not to be so verbose next time, and you could follow the same cue....

    Traveller,

    I DO want the ios flag to be set to binary, I wrote it to a .txt file so that I could view it once it was written (haven't got quite that far yet though) with notepad or the like. I will, once it's successful, write it to a .dat file. Perhaps there is no way of writing a .txt file in binary form, I can rectify that.

    Thank you so much, your block has helped to simplify the process a bit to me and for that I am ever so grateful.
    MS VC++ 6.0

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Code:
    	fstream theFile;
    	theFile.open("C:\\Junkdata\\Animals.txt",ios::out | ios::binary);
    
    	if(!theFile)
    	{
    		cout << "File object not created...";
    		return 999;
    	}
    	for(int a=0; a < count; a++)
    	{
    		theFile.write((char *) &Pets[a], sizeof(Animal));
            }
    	theFile.close();
    > if(a = 0)
    Should be:
    if(a == 0)

  6. #6
    Registered User
    Join Date
    May 2002
    Posts
    317
    Well, thanx for clearing that up swoopy. I really didn't know if you could write to a txt file in binary( it never really came up before). Thanx

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Traveller, yes the filename really doesn't matter, in this case it's Animals.txt, but it's really a binary file, only because of this flag: ios::binary

  8. #8
    Registered User
    Join Date
    Apr 2002
    Posts
    52
    Thanks a lot guys, I really appreciate your assistance (assistance? ah hell, you showed me how).

    To recap...

    You create the file, giving the proper pathname, etc;

    You open the file, write to it, then close it.

    Much grass,

    F
    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