File reading/writing question

This is a discussion on File reading/writing question within the C++ Programming forums, part of the General Programming Boards category; Allright, after a bit of messing around with the code (and your second link, vart) I've come to create the ...

  1. #16
    Registered User Rider's Avatar
    Join Date
    Nov 2005
    Posts
    28
    Allright, after a bit of messing around with the code (and your second link, vart) I've come to create the following:

    Code:
    while (Fileop.good())	// As long as there's no Error or End of File
    	  {
    
    		if ( firstrun == false ) { DestFil.write(reinterpret_cast < char * > (&och), sizeof(och)); };
    
    		Fileop.read(reinterpret_cast < char * > (&ich), sizeof(ich));
    		asc = (int)ich;
    
    			// Encrypt ASC here preparing it for output
    			asc = asc + 1;
    		och = (unsigned char)asc;
    
    		if ( firstrun == true ) { firstrun = false; };
    
    	  };
    (Asc is an INT, ich and och both Unsigned CHAR's, firstrun is a boolean)

    But something goes wrong, because the file being encrypted is reduced in size greatly (My 413KB file became 3.50KB) and I can't quite figure out where I'm messing up.

    My guess would be that something gets 'lost in translation' while being converted back and forth from char to int to char again, but like I said, I don't know exactly what's going awry here.

    Any help on the matter is greatly appreciated

  2. #17
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    where ich and och are defined?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #18
    Registered User Rider's Avatar
    Join Date
    Nov 2005
    Posts
    28
    They're defined above that, I didn't think it'd be that important, but regardless, this is where they are defined:

    Code:
    	ifstream Fileop;
    	ofstream DestFil;
    	char Filenam[32];
    	char OutFile[32];
    	unsigned char ich;
    	unsigned char och;
    	int asc;
    	bool firstrun = true;
    	long begin,end;

  4. #19
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    1. you can remove reinterpreter cast
    2. you can remove ; after }
    3. you should check the number of bytes written by the write function, if it is 0 - you should check what charaster cause this
    4. have you tried to write the file without changing value? it is working as should?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #20
    Registered User Rider's Avatar
    Join Date
    Nov 2005
    Posts
    28
    Regarding 1, I'm not quite sure what the right way would be to go about that, just removing it b0rks the code

    on 3, I made the program output the size of every character read, and already it's coming up short on bytes...

    4. I've done the following:

    Just reading and writing without converting the values to another type of variable (basically the example provided by BobS0327) and it works fine, the entire file is duplicated.

    Reading and writing without changing the value, but WITH converting it from the read procedure through all the variables, and back to the write procedure. This didn't work.

  6. #21
    Registered User Rider's Avatar
    Join Date
    Nov 2005
    Posts
    28
    *Bump*

    I'd really like some help on this issue... my current Encryption program looks like this:

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    int main (char*)
    {
    
    
    	ifstream Fileop;
    	ofstream DestFil;
    	char Filenam[32];
    	char OutFile[32];
    	unsigned char ich;
    	unsigned char och;
    	int asc;
    	bool firstrun = true;
    	long begin,end;
    
    	cout << "Encryptor\n-----------\nCreated by:Rider Rockon";	// A little bit of interface
    
    	cout << "\n\n\nFile to Encrypt:> ";	
    	cin >> Filenam;				// Wait for input, make Filenam whatever was typed
    	cout << "Destination File:> ";
    	cin >> OutFile;				// Wait for input, make OutFile whatever was typed
    
    cout << "\n Opening Input: " << Filenam << " and Output: " << OutFile << "\n";
    
    	Fileop.open (Filenam);
    	DestFil.open(OutFile,ios::trunc);
    
    	// Count File size & Return the cursor to the start of the file
    	begin = Fileop.tellg();
    	Fileop.seekg (0, ios::end);
    	end = Fileop.tellg();
    	Fileop.seekg (0, ios::beg);
    
    	cout << "Encrypting " << (end-begin) << " bytes of Data...\n";
    
    	while (Fileop.good())	// As long as there's no Error or End of File
    	  {
    
    		if ( firstrun == false ) { DestFil.write(reinterpret_cast < char * > (&och), sizeof(och)); };
    
    		Fileop.read(reinterpret_cast < char * > (&ich), sizeof(ich));
    		asc = (int)ich;
    
    			// Encrypt ASC here preparing it for output
    			asc = asc + 1;
    		och = (unsigned char)asc;
    
    		if ( firstrun == true ) { firstrun = false; };
    
    	  };
    
    cout << "\nDone...\n";
    
    Fileop.close();
    DestFil.close();
      return 0;
    }
    as stated above, the file being encrypted is reduced in size greatly (My 413KB file became 3.50KB) and I can't quite figure out where I'm messing up.

    Could anyone please help me out? I'm just a beginning programmer and I haven't got too much experience so far... heck... I'm suprised I even got this far!

    Any help on the matter would be greatly appreciated

  7. #22
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    hmmm...
    Have you tried the suggestion of the BobS0327?
    Code:
    ifstream MyInFile (argv[1], ios::in | ios::binary);
    ofstream MyOutFile (argv[2], ios::out | ios::binary);
    Last edited by vart; 12-22-2006 at 06:50 AM.
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  8. #23
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Code:
    int main (char*)
    Is this legal?
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  9. #24
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    No, most definitely not.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  10. #25
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,496
    The first step in writing any file transformation program is to simply write something which copies the input to the output without changing it at all. Until you can manage that, the rest of the problem doesn't matter.

    Code:
    #include<iostream>
    #include<fstream>
    using namespace std;
    
    int main()
    {
        ifstream Fileop ( "a.exe", ios::in | ios::binary );
        ofstream DestFil( "copy.bin", ios::out | ios::binary );
        char ch;
    
        while ( Fileop.get(ch) ) {
            DestFil.put(ch);
        }
    
        Fileop.close();
        DestFil.close();
    
        return 0;
    }
    
    
    $ # copying itself
    $ ls -l a.exe ; ./a.exe ; ls -l copy.bin ; cmp a.exe copy.bin
    -rwxr-xr-x 1 who None 477334 Dec 22 17:34 a.exe
    -rw-r--r-- 1 who None 477334 Dec 22 17:35 copy.bin
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  11. #26
    Registered User Rider's Avatar
    Join Date
    Nov 2005
    Posts
    28
    Yes well, I already accomplished that earlyer. The only problem is that my method of encryption (by recalculating the ASCII value of the read character) requires the character to be moved through other variables... somewhere in that particular process, something goes wrong.

    This routine:

    Code:
    while (Fileop.good())	// As long as there's no Error or End of File
    	  {
    
    		if ( firstrun == false ) { DestFil.write(reinterpret_cast < char * > (&och), sizeof(och)); };
    
    		Fileop.read(reinterpret_cast < char * > (&ich), sizeof(ich));
    	//	asc = (int)ich;
    
    Fileop.read(reinterpret_cast < char * > (&pos), sizeof(pos));
    
    			// Encrypt ASC here preparing it for output
    	//		asc = asc + 1;
    	//	och = (unsigned char)asc;
    
    		if ( firstrun == true ) { firstrun = false; };
    
    	  };
    This was the original routine, kudos to BobS0327, and it worked fine... But I'll try your version Salem... it looks a lot more compact at least (and, for a coding newb like me, a lot more clear on what it does )

  12. #27
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,496
    Well it's really rather simple,

    From this
    Code:
        while ( Fileop.get(ch) ) {
            DestFil.put(ch);
        }
    To this
    Code:
        while ( Fileop.get(ch) ) {
            ch = myEncrypt(ch);
            DestFil.put(ch);
        }
    Before messing with files, you can test the function with very basic tests like
    Code:
    int main ( ) {
        cout << myEncrypt('A');
        // or
        for ( int i = 'A' ; i <= 'Z' ; i++ ) cout << myEncrypt( i );
    }
    When both ideas are working well separately, then you can try bringing them together.

    It doesn't hurt to have a couple of 'test' projects on the side containing just a main(), where you can work on a specific idea in isolation. When you've got it working, then simply copy/paste what you want to the real project.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  2. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Hmm....help me take a look at this: File Encryptor
    By heljy in forum C Programming
    Replies: 3
    Last Post: 03-23-2002, 09:57 AM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 02:58 PM

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