Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. fstream, fail()

fstream, fail()

Scheduled Pinned Locked Moved C / C++ / MFC
questioniossecurityhelptutorial
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    James_722
    wrote on last edited by
    #1

    I am trying to create a simple to program to encrypt a file but I am having a lot of trouble with accessing the file. Firstly, whenever I try to access the file fstream's fail() always returns true. If I take the check to fail() from my code it works fine encrypting the file but when I go to decrypt it there are artifacts in the file (2 extra bytes in my test file). here is the code in question:

    #define macro_encrypt (int) Byte + 25
    #define macro_decrypt (int) Byte - 25

    void fn_Decrypt()
    {
    char oldFilename[200];
    char newFilename[200];

    cout << "Enter Filename:   ";
    cin >> oldFilename; cout << endl;
    cout << "Enter new Filename:   ";
    cin >> newFilename; cout << endl;
    
    ifstream infile;
    ofstream outfile;
    char Byte;
    
    infile.open(oldFilename, ios::in | ios::binary);
    outfile.open(newFilename, ios::out | ios::binary);
    
    while (!infile.eof())
    {
        char NewByte;
    
        Byte = infile.get();
    
        NewByte = macro\_decrypt;
    
        outfile.put(NewByte);
    }
    
    infile.close();
    outfile.close();
    fn\_start();
    

    }

    I input a text file:

    hello encryption

    I get this (notepad output):

    ~……ˆ9~‡|‹’‰‚ˆ‡

    And when I go to decrypt that file I get this:

    hello encryptionÿæ

    What makes this all the more frustrating is that when I compile the code this is based on it works fine. I can't see why it would work and mine won't, they are functionally identical (from what I can see). Does anyone know why this is happening and/or how to fix it?

    A 1 Reply Last reply
    0
    • J James_722

      I am trying to create a simple to program to encrypt a file but I am having a lot of trouble with accessing the file. Firstly, whenever I try to access the file fstream's fail() always returns true. If I take the check to fail() from my code it works fine encrypting the file but when I go to decrypt it there are artifacts in the file (2 extra bytes in my test file). here is the code in question:

      #define macro_encrypt (int) Byte + 25
      #define macro_decrypt (int) Byte - 25

      void fn_Decrypt()
      {
      char oldFilename[200];
      char newFilename[200];

      cout << "Enter Filename:   ";
      cin >> oldFilename; cout << endl;
      cout << "Enter new Filename:   ";
      cin >> newFilename; cout << endl;
      
      ifstream infile;
      ofstream outfile;
      char Byte;
      
      infile.open(oldFilename, ios::in | ios::binary);
      outfile.open(newFilename, ios::out | ios::binary);
      
      while (!infile.eof())
      {
          char NewByte;
      
          Byte = infile.get();
      
          NewByte = macro\_decrypt;
      
          outfile.put(NewByte);
      }
      
      infile.close();
      outfile.close();
      fn\_start();
      

      }

      I input a text file:

      hello encryption

      I get this (notepad output):

      ~……ˆ9~‡|‹’‰‚ˆ‡

      And when I go to decrypt that file I get this:

      hello encryptionÿæ

      What makes this all the more frustrating is that when I compile the code this is based on it works fine. I can't see why it would work and mine won't, they are functionally identical (from what I can see). Does anyone know why this is happening and/or how to fix it?

      A Offline
      A Offline
      Aescleal
      wrote on last edited by
      #2

      The artifacts are due to the way you're controlling the loop. Check what std::istream::get actually returns. Note that your cyphertext already has an extra character in it so your encrypt is part of the problem. Just out of interest, as you're programming in C++ and not C: - get rid of all the explicit opens/closes, you don't need them, destructors are wonderful things - instead of letting a user crash your code by entering a filename of longer than 200 characters use std::strings and std::getline - don't use macros, a pair of functions would have worked as well and enabled you to use the same code for encryption and decryption (the same goes for C BTW) I'd also consider splitting the encrypt/decrypt bits of your code from all the guff asking for filenames, then you'd have something you can unit test without all the nuts ache of entering files all the time. Cheers, Ash

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups