Thread: Converting text file to binary

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    84

    Converting text file to binary

    Hi, Im trying to write a program to read in a textfile and convert it its binary represntation. I was under the impression that c++ does this automatically if you construct an ofstream using ios_base::binary. Can someone tell me how to do this properly ? here is my code -

    Code:
    #include<iostream>
    #include<fstream>
    #include<string>
    
    using namespace std;
    
    int main()
    {
    	string filename;
    
    	cout << "Enter Filename: ";
    	cin >> filename;
    
    	ifstream infile;
    	infile.open(filename.c_str());
    	if(infile.fail()){
    		cout << "File not found\n"; exit(1);
    	}
    	else {
    		cout << "Converting file..." << endl;
    	}
    
    	filename += "_b.txt";
    	ofstream outfile(filename.c_str(),ios_base::binary);
    	
    	string buffer;
    	while(getline(infile,buffer))
    		outfile << buffer;
    
    	cout << "File Conversion Complete.." << endl;
    
    	outfile.close();
    	infile.close();
    
    	return 0;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Why do you not like your answer? (You might be stripping newlines out, perhaps.)

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    84
    Oh sorry! I forgot to say, The program works fine except that the new file it creates is not in binary. It is same as the input file, however I want it to be non human readable.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So you're going to have to do something else. Binary files can be human readable, and if all they contain is text, then they very well will be. You will need to do some encrypting.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    84
    Oh, well now I'm confused. What exactly is the point of ios_base::binary then ? I checked both my files and they are absolutely identical and have the same file size as well.
    Also, I remember wirting an array to a file in c once using the "wb" mode and all I could see when I opened the file was random characters..that is pretty much what I'm trying to do here. What is the reason for the unreadable text in the array ? and could someone tell me some simple encryption methods or point me to some good information/tutorial ? thanks!

  6. #6
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    Every files are "binary files". Text files are just a special case of binary files, where all the content are characters in some character encoding.

    Also, if you are looking for a "simple" way to do encryption, you would be better to look for some library that does the work for you, or using an external program. Else, you might want to look at "Vigenere cipher", which is really simple to implement and will make your file looks like gibberish, but is breakable in most case.
    I hate real numbers.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by rocketman03 View Post
    Oh, well now I'm confused. What exactly is the point of ios_base::binary then ? I checked both my files and they are absolutely identical and have the same file size as well.
    Also, I remember wirting an array to a file in c once using the "wb" mode and all I could see when I opened the file was random characters..that is pretty much what I'm trying to do here. What is the reason for the unreadable text in the array ? and could someone tell me some simple encryption methods or point me to some good information/tutorial ? thanks!
    In binary you are printing things exactly as they are stored in the computer. For text, that's not a surprise, since text is already stored in ASCII. For something like a double, what is printed (normally) as 4.75 would be stored internally as 0x40980000, which you might see as "(b ".

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  2. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  3. How to use FTP?
    By maxorator in forum C++ Programming
    Replies: 8
    Last Post: 11-04-2005, 03:17 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  5. Outputting String arrays in windows
    By Xterria in forum Game Programming
    Replies: 11
    Last Post: 11-13-2001, 07:35 PM