Binary file problems..

This is a discussion on Binary file problems.. within the C++ Programming forums, part of the General Programming Boards category; Okkk, here goes. This is my code so far, and it appears my binary file either isn't working right, or ...

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    34

    Binary file problems..

    Okkk, here goes. This is my code so far, and it appears my binary file either isn't working right, or just isn't outputing to my outFile (i'll go for probably the first option. Here is my code:
    Code:
    Binary.h
    #include<iostream>
    #include<fstream>
    
    using namespace std;
    
    class BinaryFile
    {
    public:
    	void readFromFile(ifstream& inFile);
    	void validRead(BinaryFile rd);
    	void writeBinary(BinaryFile rd, ofstream& outFile);
    	void readBinary(BinaryFile rd, ifstream& inFile, ofstream& outFile);
    	void outputRd(ofstream& outFile, BinaryFile rd);
    
    	int acctNum;
    	float acctBalance;
    };
    
    Binary.cxx
    #include "Binary.h"
    
    
    void BinaryFile::readFromFile(ifstream& inFile)
    {
    	int holder;
    	holder = inFile.peek();
    //	if (holder = -1)
    //		continue;
    //	else
    		inFile >> acctNum >> acctBalance;
    }
    
    void BinaryFile::writeBinary(BinaryFile rd, ofstream& outFile)
    {
    	outFile.seekp((rd.acctNum % 97) * sizeof(rd));
    	outFile.write((char*)&rd, sizeof(rd));
    }
    
    void BinaryFile::readBinary(BinaryFile rd, ifstream& inFile, ofstream& outFile)
    {
    	inFile.read((char*)&rd, sizeof(BinaryFile));
    	while(!inFile.eof())
    	{
    		if (acctNum != 0)
    		{
    			outFile << acctNum << acctBalance;
    		}
    		inFile.read((char*)&rd, sizeof(BinaryFile));
    	}
    }
    
    void BinaryFile::outputRd(ofstream& outFile, BinaryFile rd)
    {
    	outFile << acctNum << acctBalance;
    }
    
    runBinary.cxx
    #include "Binary.h"
    
    
    int main()
    {
    	ifstream inFile;
    	ofstream outFile;
    	inFile.open("in.txt");
    	outFile.open("binrecords.bin", ios::out | ios::binary);
    	int blank;
    
    	istream& read(char array[], int numberOfChar);
    	ostream& write(const char array[], int numberOfChar);
    
    	BinaryFile blankRd;
    	for (int i = 1; i <= 100; i++)
    	{
    		outFile.write((char*)&blankRd, sizeof(blankRd));
    		cout.write((char*)&blankRd, sizeof(blankRd));
    	}
    	BinaryFile rd;
    	while(!inFile.eof())
    	{
    		rd.readFromFile(inFile);
    		cout << rd.acctNum << rd.acctBalance << endl;
    		rd.writeBinary(rd,outFile);
    	}
    	inFile.close();
    	outFile.close();
    	inFile.open("binrecords.bin", ios::in);
    	outFile.open("out.txt");
    	rd.readBinary(rd, inFile, outFile);
    
    	return 0;
    }
    Ok, now I know that it is reading correctly from an inFile, but that's about it. I don't know if it's inputting correctly into the binary file (or if the binary file is created correctly), or inputting correctly into the outFie. What the program has to do is take numbers form an inFile (acctNum and acctBalance) and put them into a binary file that is 100 records long. I then have to output them sequentially and randomly, and put in an "empty" when there is nothing there. Example of the output:
    0) 22 $22.36
    1) 33 $333.33
    2) Empty
    3) Empty
    4) 58 $5.23
    etc.....
    Last edited by advocation; 09-27-2005 at 12:27 PM.

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    I know it's commented out, but still:
    Code:
    //	if (holder = -1)
    In your read function, print out each line that's read in, so you can see if it's what you want.

    What's in the output file?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    34
    Nothing shows up in my outFile. I know that it is inputting fine (not positive if inputting into the binfile right though) because I did a cout before it inputted into the binfile. I think it might be a problem retrieving the data from the inFile.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Okay, so print each line as you read it in. Then you can tell if you read in the data or not.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User
    Join Date
    Mar 2005
    Posts
    34
    I did, right before it's inputted into the binary file it reads ok. I can't read line from line in my binary file because, for one, i can't just manually read it, two, i don't know if my code is working right to read it out of the binary file. I actually meant i think my problem is retrieving data from my binfile.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 05:27 PM
  2. Replies: 7
    Last Post: 04-27-2009, 06:56 PM
  3. Problems with reading the end of a binary file
    By firyace in forum C Programming
    Replies: 4
    Last Post: 07-09-2007, 05:44 PM
  4. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 01:13 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 11:12 PM

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