Thread: Binary OFSTREAM

  1. #1
    Confused
    Join Date
    Nov 2002
    Location
    Warwick, UK
    Posts
    209

    Binary OFSTREAM

    Code:
    #include <windows.h>
    #include <iostream.h>
    #include <fstream.h>
    #include <string>
    
    ofstream outfile;
    ifstream infile;
    
    char name[256];
    char contents[200];
    
    int main()
    {
    cout << "Name your file. Press ENTER when finished." << endl << ">> ";
    cin.getline(name, 256, '\n');
    cout << "\n\nNow type the contents. Press * and ENTER when finished.\n>> ";
    cin.getline(contents, 200, '*');
    outfile.open(name);
    outfile << contents, ios::binary;
    outfile << "\n\n\nDid it work ? Hell no, as usual...";
    outfile << "\n\nCan you read this ?", ios::binary;
    return 0;
    }
    Can anyone tell me why I'm not getting ones and zeros in my created text file ?

    Thanks

  2. #2
    Me -=SoKrA=-'s Avatar
    Join Date
    Oct 2002
    Location
    Europe
    Posts
    448
    I think your problem lies in the way you open the file. It shuld be something like:
    Code:
    outfile.open(name, ios::in ¦ ios::binary):
    Not have my book here but try with that.
    SoKrA-BTS "Judge not the program I made, but the one I've yet to code"
    I say what I say, I mean what I mean.
    IDE: emacs + make + gcc and proud of it.

  3. #3
    Confused
    Join Date
    Nov 2002
    Location
    Warwick, UK
    Posts
    209
    It doesn't work Still I get readable text, no binary.

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    What are you expecting to see in the file? You won't see a bunch of 1's and 0's just because you are opening the file in binary mode. You are writing normal text to the file and that is how it is going to look in the file even if it is open in binary mode. The only difference binary mode is likely going to make is when you then try to read the file back in using another program. If you were to open a file in text mode, any two character carriage-return/linefeed character combinations will be read in as a single '\n' character. Also, reading in any EOF character/CTRL-Z character will fool your program into thinking there is no more to the file even though there may be tons more data actually present. Opening the file in binary mode prevents these things from happening.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Confused
    Join Date
    Nov 2002
    Location
    Warwick, UK
    Posts
    209
    I think what I'm after doesn't have the same name to CPP good people and I. I just wanted to make a file into binary, so when I open it, I can't read it. Basically what I wanted is a very low level encryption that people who aren't good with computers can't read ( or anyone without a binary to text converter ).

    Thanks for bringing it to attention, hk_mp5kpdw ! Next time, I'll explain stuff a little more cuz people didn't understand it

  6. #6
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    ha, this is funny. I really must point out that ascii IS binary. All files are binary! if you write out a string to a file you get a string! you have do do some conversion on the ascii there chief. if you want something simple, just shift the ascii values or something.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  7. #7
    Confused
    Join Date
    Nov 2002
    Location
    Warwick, UK
    Posts
    209
    Heh, yes, I knew that Brain, but the thing is I want it to be opened, when I double click the .txt, as ones and zeros, or as numbers, or as low level encryption. Think of a saved game : I don't want people to create a save, edit it in text and reload the edited one.

  8. #8
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Here is a simple program I made a long time ago that uses some simple XOR encryption with a fixed key that will take a plain-text file as input and create a garbled cypher-text (encrypted file as output). You can also use the same program, using the encypted file as input and a new file as output, to create an unencrypted file from the encrypted file. There are many programs out there that do similar things and I don't make any claims that this one is anywhere near the best.
    Code:
    #include <iostream>
    #include <fstream>
    
    using std::cout;
    using std::endl;
    using std::ifstream;
    using std::ofstream;
    using std::ios;
    
    int main( int argc, char* argv[] )
    {
    	char cValue;					// "char" I/O buffer
    	int iIndex = 0;					// Looping variable
    	const char* key = "Ob1k-%Zpf4[";		// Can be anything!
    	ifstream in_file;				// Input file
    	ofstream out_file;				// Output file
    
    	// Check for proper number of command-line arguments.
    
    	if( argc != 3 )
    	{
    		cout << "Improper number of command line arguments." << endl;
    		return 1;
    	}
    
    	// We have the proper number of command-line arguments, process files.
    
    	else
    	{
    		// Open the two files for input and output.
    
    		in_file.open( argv[1], ios::binary | ios::in );
    		out_file.open( argv[2], ios::binary | ios:: out );
    
    		// Test to make sure input/output files are open.
    
    		if( !in_file )
    		{
    			cout << "Could not open input file." << endl;
    			return 1;
    		}
    
    		if( !out_file )
    		{
    			cout << "Could not open output file." << endl;
    			return 1;
    		}
    
    	}
    
    	// Encrypt data from the input file and send to the output file using the
    	// default key.
    
    	while( in_file.get( cValue ) )
    	{
    		out_file.put((char)(cValue^key[iIndex++]));
    		if( iIndex == strlen(key) ) iIndex = 0;
    	}
    
    	// Clean up and exit program.
    
    	in_file.close();				// Close input file
    	out_file.close();				// Close output file
    	return 0;
    
    }	// End of "main" function.
    For example, naming the program "encrypt" and calling it like this:

    encrypt plain.txt cypher.txt

    Will create an encrypted file named cypher.txt using the data from plain.txt that should be unreadable. If you then run the program again like this:

    encrypt cypher.txt plain2.txt

    It will create an unencrypted file named plain2.txt using the encrypted data from within the cypher.txt file where plain2.txt and the original plain.txt files should be exactly equal.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  9. #9
    Confused
    Join Date
    Nov 2002
    Location
    Warwick, UK
    Posts
    209
    Thanks. I don't understand that code at all... can someone please explain what's going on in it ? It would really help !

    Also, where do I tell the program what I want coded ( what file ) ?

    Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 11-04-2006, 11:07 AM
  2. Capturing key presses
    By cgod in forum Windows Programming
    Replies: 6
    Last Post: 11-25-2004, 01:10 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Tutorial review
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-22-2004, 09:40 PM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM