"Stray Character" when running a simple encryption program.

This is a discussion on "Stray Character" when running a simple encryption program. within the C++ Programming forums, part of the General Programming Boards category; I wrote this simple program for my little cousin who wanted to see something useful done with a computer. The ...

  1. #1
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,526

    "Stray Character" when running a simple encryption program.

    I wrote this simple program for my little cousin who wanted to see something useful done with a computer.
    The bug it has is not much problem when 'encrypting' (sort of) text files.
    The problem it has is that every time I encrypt a file and decrypt it, an extra character is introduced in the last line.Not a single of the individual functions (apparently) show this behaviour when tested individually .

    I'm pasting the whole code for convenience.
    [Note that you'd have to apply a c_str() to strings when used with the fstream open functions , if your compiler does not support c++0x/11 features. If it does, remember to tell it to explicitly do so...by something like gcc's -std=c++0x flag]

    Code:
    #include<fstream>
    #include<iostream>
    using namespace std;
    
    
    void encryption(string& data,string passkey);
    void decryption(string& data,string passkey);
    string get_file_input(string filename);
    void output_to_file(const string& data, string filename); 
    bool quit_condition();
    void get_input(string&,string&,string&,string&);
    
    
    int main(int argc,char** argv)
    { 
        string in_file_name,out_file_name,data,passkey;
        string enc_or_dec;
        bool input_ready;
    
        if(argc==1)input_ready=false;
        else
        {
        in_file_name = string(argv[1]);
        out_file_name = string(argv[2]);
        passkey = string(argv[3]);
        enc_or_dec = string(argv[4]);
        input_ready = true;
        }
    
        while(true)
        {
        if(input_ready); 
        else
            get_input(in_file_name,out_file_name,passkey,enc_or_dec);
        
        data = get_file_input(in_file_name);
        
        if(enc_or_dec[0]=='e'||enc_or_dec[0]=='E')
            encryption(data,passkey);
        else if(enc_or_dec[0]=='d'||enc_or_dec[0]=='D')
            decryption(data,passkey);
        else
        {
            cout<<"Enter ( E or D )! .*****."<<endl;
            if(!quit_condition())break;
            else continue;
        }
        output_to_file(data,out_file_name);
        
        if(input_ready)break;
        //^The Program doesn't allow the user to continue after supplying cmdline args
        if(!quit_condition())break;
        }
        return 0;
    }
    string get_file_input(string filename)
    {
        ifstream is(filename);
        if(!is)
        {
        cout<<"File "<<filename<<" not found. Please try again.\n";
        return "";
        }
        else if(is.fail())
        {
        cout<<"Opening "<<filename<<" failed for some reason. Please try again.\n";
        return "";
        }
        string line,data("");
        do
        {
        getline(is,line);
        data = data+line+"\n";
        
        }while(!is.eof());
        return data;
    }
    void output_to_file(const string& data, string filename)
    {
        ofstream os(filename);
        os<<data;
    }
    bool quit_condition()
    {
        string q;
        cout<<"Quit?\n$ ";
        //q=getchar();
        cin>>q;
        if(q[0]=='y'||q[0]=='Y')
        return false;
        else return true;
    }
    void encryption(string& data,string passkey)
    {
        int data_end(data.size()),passkey_bound(passkey.size());
        
        for(int counter=0;counter<data_end;counter++)
        {
        data[counter]+=passkey[counter%passkey_bound];
        }
    }
    void decryption(string& data,string passkey)
    {
        int data_end(data.size()),passkey_bound(passkey.size());
        
        for(int counter=0;counter<data_end;counter++)
        {
        data[counter]-=passkey[counter%passkey_bound];
        }
    }
    
    
    void get_input(string& in_file_name,string& out_file_name,string& passkey,string& enc_or_dec)
    {
        cout<<"Enter Input filename:\n$ ";
        cin>>in_file_name;
        cout<<"Enter Output filename:\n$ ";
        cin>>out_file_name;
        cout<<"Enter Passkey:\n$ ";
        cin>>passkey;
        cout<<"Enter ( E or D ) for enc/dec:\n$ ";
        cin>>enc_or_dec;
    }
    There is also a problem with an extra newline in the get_file_input function. I am also looking for a way to remove the last newline.
    Last edited by manasij7479; 08-06-2011 at 10:05 AM.
    Manasij Mukherjee | gcc-4.9.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,824
    SourceForge.net: Feof - cpwiki

    Code:
        do
        {
        getline(is,line);
        data = data+line+"\n";
        
        }while(!is.eof());
    Try it with an empty file, you'll get a return string of a single \n

    What you should have is
    while ( getline(is,line) ) data = data+line+"\n";
    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.

  3. #3
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,526
    What you should have is
    while ( getline(is,line) ) data = data+line+"\n";
    Ok..thanks
    Last edited by manasij7479; 08-06-2011 at 10:22 AM.
    Manasij Mukherjee | gcc-4.9.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 46
    Last Post: 08-24-2007, 05:52 PM
  2. running programs as "hidden" or in the "background"
    By Leeman_s in forum C++ Programming
    Replies: 4
    Last Post: 12-18-2002, 03:47 AM
  3. "Stray pointer" question
    By CppNewbie in forum C++ Programming
    Replies: 1
    Last Post: 01-29-2002, 01:39 PM

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