Thread: C++ File I/O question

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    2

    C++ File I/O question

    Well, Im trying to write a simple programm for encrypting and decrypting source files(C++,Java), for my programming class. I read one character at a time and use XOR encryption(from the tutorials) and copy that one character to another file. The problem is that it wont copy the entire file, only about a half of it, and im not able to figure it out since i newer really did any FILE I/O at all. Heres a basic idea behind the code:
    Code:
          //key is a password string
         //z is a counter for key
           ostream a_file(filename,ios::in);
           istream b_file(finename2);
           while ( a_file)
    	{
    		x=a_file.get();
    		z++;
    		if(z>4)  z=0;
    		y=char(x^key[z]);
    		b_file.put(y);
    	}
    Thats the basic idea behind the code, any help would be apreciated.

    PS:
    here's the original file:
    Code:
    #include<iostream.h>
    #include<fstream.h>
    
    void main(int argc,char *argv[])
    {
    	 cout<<argv[0]<<'\n';
    
    
    	char x;
    	char y;
    	char key[5];
    	int z=0;
    	char file_a[10];
    	char file_b[10];
    
    	cout<<"Please enter the name of the source file\n";
    	cin.get(file_a,10);
    	cin.ignore(80,'\n');
    	cout<<"Now please enter the name of destination file\n";
    	cin.get(file_b,10);
    	cin.ignore(80,'\n');
    	cout<<"Please enter yor 5 letter key\n";
    	cin.get(key,5);
    	cin.ignore(80,'\n');
    	ifstream a_file(file_a,ios::in);
    	ofstream b_file(file_b);
    	
    	while ( a_file)
    	{
    		x=a_file.get();
    		z++;
    		if(z>4)  z=0;
    		y=char(x^key[z]);
    		b_file.put(y);
    	}
    
    	cout<<"Operation finished sucessfully!\n";
    	a_file.close();
    	b_file.close();
    }
    after XORing etc., this is what i copies:
    Code:
    #include<iostream.h>
    #include<fstream.h>
    
    void main(int argc,char *argv[])
    {
    	cout<<argv[0]<<'\n';
    
    {
    	char x;
    	char y;
    	char key[5];
    	int z=0;
    	char file_a[10];
    	char file_b[10];
    
    	cout<<"Please enter the name of the source file\n";
    	cin.get(file_a,10);
    	cin.ignore(80,'\n');
    	cout<<"Now please enter the name of destination file\n";
    	cin.get(file_b,10);
    	cin.ignore(80,'\n');
    	cout<<"Please enter yor 5 letter key\n";
    	cin.get(keś›//!!!wtf???

  2. #2
    Banned
    Join Date
    Jun 2005
    Posts
    594

    Post

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
    	string key = "Asdfasdghafdhadfhadf"; //must include <string> to use
    	string filenamein = "file.txt";
    	string filenameout = "newfile.txt";
    	ifstream in(filenamein.c_str(), ios::binary);
    	ofstream out(filenameout.c_str(), ios::binary);
    	char ch, y;
    	if(in.is_open())
    	{
    		for(int z = 0; in.read( (char*)&ch, sizeof(ch) ) ; z++)
    		{
    			if(z == key.length())
    			{
    				z = 0;
    			}
    			y = ch^key[z];
    			out.write (  (char*)&y, sizeof(y) );
    		}
    	}
    	in.close();
    	out.close();
    	return 0;
    }
    also iostream.h is outdated it should be iostream
    void main is a no no, and your posting in c++ you should
    be using strings instead of char arrays.


    your problem most likely has to do because your not opening /
    reading it as binary and writing as binary.
    Last edited by ILoveVectors; 08-15-2005 at 09:59 PM.

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    2
    Thank you your code and advice has helped me alot i really apreciate it.

  4. #4
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    As Vectors correctly mentioned, the problem is likely to be that you are not reading/writing the data in binary format. This may be corrupting your data and preventing you from reading in the entire file. (See above link).

    http://www.delorie.com/djgpp/v2faq/faq9_4.html

    In most cases you don't have to worry about reading in files using as binary format. However, encryption programs more or less demand this.

    Second you have to be careful to encrypt all your data, i.e ensure that each bit is xor'ed with the corresponding letter in your key, otherwise part of your key may be revealed in your encrypted file. (Try and encrypt a mp3 file and then view the file with notepad, ) Of course there is an easy way to solve this. I'll leave this for you to think about.

    Third, whilst this just what you wanted, bear in mind you have implemented the most simplest case of xor encryption. It would be wise to add further complexity to your algorithmn to ensure increased security. (See DES encrytion etc.)

    If, on the other hand, this is just be for home computer use you can consider your data secure:- Mommy and daddy probably don't even know what 'xor' encryption is let alone know how to decode it

    And lastly, if this is your own project, you may want to continue by looking into compression algorithms, namely the 'Huffman compression algorithm.'

    What these do is compress your file so that it takes up less space than the original. Sounds impossible? Have a look and learn some more.

    thanks treenef.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. File i/o and ASCII question
    By muzihc in forum C Programming
    Replies: 13
    Last Post: 11-04-2008, 11:46 PM
  3. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  4. File I/O Question
    By Achy in forum C Programming
    Replies: 2
    Last Post: 11-18-2005, 12:09 AM
  5. Another dumb question about file i/o
    By Cobras2 in forum C++ Programming
    Replies: 23
    Last Post: 03-14-2002, 04:15 PM