Output file has extra characters.

This is a discussion on Output file has extra characters. within the C++ Programming forums, part of the General Programming Boards category; I am writing a program that encrypts a text file and then reopens the encrypted text and deciphers it and ...

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    38

    Output file has extra characters.

    I am writing a program that encrypts a text file and then reopens the encrypted text and deciphers it and prints to another text file. Problem is at the end it gives me these two characters:  §. I was thinking it had to do with not closing the file, I even commented out the .close parts and the characters still showed up, so I think its related.

    Code:
    // left out the open output and input files and the initial encrypting file process part.
     if (outfile.is_open()&& myfile.is_open())
      {      
        while (!myfile.eof())
        {               
                 alpha =myfile.get();             
                  enk =int(alpha) -10;              
                  outfile<< char(enk) ;
                                                 
        }      
            outfile.close();
            myfile.close();
       }     
      else
             cout << "Unable to open file";

  2. #2
    Registered User
    Join Date
    Apr 2010
    Posts
    38
    I have a file that gets opened as char and converted into int + 10 for encryption, then that encrypted file gets opened and converted back to int -10 as shown above.
    For example the input file will read "its cheasy" and the out put will say "its cheasy §"

    So I think that maybe niether time I close the files are actually closing them? I just don't see why as the format looks accurate. Thanks for any insight.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    You can start by not using eof to control your while loop. Instead test the read operation's return result and use that to control whether or not you enter the loop.

    Code:
    while(myfile.get(alpha))
    {               
        // Do stuff...
    }
    "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

  4. #4
    Registered User
    Join Date
    Apr 2010
    Posts
    38
    If I use that the output file stops at whitespace? so my sentence gets hose and the deciphered file changes from "want some cheese" to "lis e "

    I had tried a couple different input methods, EOF transmits everything excepting that extra 2 characters.

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    How are you opening the encrypted file? Encrypted files should almost always be opened in binary mode.
    "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

  6. #6
    Registered User
    Join Date
    Apr 2010
    Posts
    38
    By encrypted i mean converting the char to an int and adding 10 to it. The code I posted has the decrypting code.
    right now I convert it to int to add 10 and then convert back to char prior to cout to file.
    BTW thanks for your input.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 07-10-2008, 04:45 PM
  2. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 04:53 AM
  3. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 02:19 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM

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