-
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
-
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.
-
It doesn't work :( Still I get readable text, no binary.
-
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.
-
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 :D cuz people didn't understand it :(
-
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.
-
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.
-
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.
-
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