Thread: how to add save file func into this code!!

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    3

    Question how to add save file func into this code!!

    Desc: this is an Text Encryption and decryption program that ihave complete coding. now i need to add another function

    Q1. save the encrypted msg into a file, this is because later i can display the decrypted msg along with the encrypted msg to prove that the prog can be run with input and output redirection to read and write on file.

    Q2. Although this coding was completed and can be run smoothly, but there still have errors on it after compiled. the error msg is : Conversion may lose significant digits

    pls help on it. Thanks

    #include <iostream.h>
    #include <conio.h>
    #include <stdio.h>

    class String
    {
    private:
    char *string;
    int len;
    public:
    String(char * = "");
    ~String(void){delete[] string;}
    void getString(void){cout << "Plese enter your sentence : "; fflush(stdin); gets(string);}
    void display(void){cout << "You have entered as follow : "; cout << string << endl << endl;}
    int code(void);
    int decode(void);
    };

    String::String(char *s)
    {
    len=strlen(s)+1;
    string=new char[len];
    strcpy(string,s);
    }

    int String::code()
    {
    char *temp=string;
    char sample[96]={'0','1','2','3','4','5','6','7','8','9','A','B', 'C','D',
    'E','F','G','H','I','J','K','L','M','N','O','P','Q ','R',
    'S','T','U','V','W','X','Y','Z','a','b','c','d','e ','f',
    'g','h','i','j','k','l','m','n','o','p','q','r','s ','t',
    'u','v','w','x','y','z','!','"','#','$','%','&','\ \',',',
    '(',')','*','+',',','-','.','/',',',':',';','<','=','>',
    '?','@','[','\\',']','^','_','`','{','|','}','~'};
    char code[96] ={'E','S','c','r','#','/','[','H','R','h','5','D','Q','b',
    'p','"','.','^',',','0','K','W','k','x',')','1','A ','P',
    'd','v','%','-',']','9','J','U','m','o','!',',','\\','4',
    'G','T','i','u','&',';','{','7','C','V','f','w','$ ','_',
    ':','8','M','X','g','s','(','\\',',','+','|','6',' B','O',
    'a','n','*','=','3','F','Y','z','q','@','`','2','N ','e',
    'z',' ','~','I','l','+','?','}','L','y','<','>',};
    int count=0;
    for(*temp;*temp!=NULL;*temp++)
    {
    while(*temp!=sample[count])
    count++;
    cout << code[count];
    count=0;
    }
    cout << endl;
    return 0;
    }


    int String::decode()
    {
    char *temp=string;
    char sample[96]={'0','1','2','3','4','5','6','7','8','9','A','B', 'C','D',
    'E','F','G','H','I','J','K','L','M','N','O','P','Q ','R',
    'S','T','U','V','W','X','Y','Z','a','b','c','d','e ','f',
    'g','h','i','j','k','l','m','n','o','p','q','r','s ','t',
    'u','v','w','x','y','z','!','"','#','$','%','&','\ \',',',
    '(',')','*','+',',','-','.','/',' ',':',';','<','=','>',
    '?','@','[','\\',']','^','_','`','{','|','}','~'};
    char code[96] ={'E','S','c','r','#','/','[','H','R','h','5','D','Q','b',
    'p','"','.','^',',','0','K','W','k','x',')','1','A ','P',
    'd','v','%','-',']','9','J','U','m','o','!',',',' \\','4',
    'G','T','i','u','&',';','{','7','C','V','f','w','$ ','_',
    ':','8','M','X','g','s','(','\\',',','+','|','6',' B','O',
    'a','n','*','=','3','F','Y','z','q','@','`','2','N ','e',
    'z',' ','~','I','l','+','?','}','L','y','<','>',};
    int count=0;
    for(*temp;*temp!=NULL;*temp++)
    {
    while(*temp!=code[count])
    count++;
    cout<< sample[count];
    count=0;
    }
    cout << endl;
    return 0;
    }

    void main(void)
    {
    char again,choice;
    cout << "Welcome To Encrytion/Decryption Machine" << endl << endl;
    cout << "This program is writtten by : " << endl << endl;
    cout << "Name : Cha Woo Chuan" << endl;
    cout << "Student ID : 020191011348" << endl << endl;
    cout << "For term 1 2002 C++ assignment" << endl << endl << endl << endl << endl << endl << endl;
    cout << "Please press any key to continue...........";
    getche();
    String sentence;
    do
    {
    do
    {
    clrscr();
    cout << endl << "************************" << endl;
    cout << "Please choose 'E' or 'D'" << endl;
    cout << "E. Encrypt" << endl << "D. Decrypt" << endl ;
    cout << "************************" << endl;
    cin >> choice;
    }
    while((choice!='E')&&(choice!='D'));
    cout << endl << endl;
    sentence.getString();
    sentence.display();
    switch(choice)
    {
    case 'E' : cout << "The encrypted message is : ";
    sentence.code();
    break;

    case 'D' : cout << "The decrypted message is : ";
    sentence.decode();
    break;

    default: cout << "Wrong input!\n";
    }
    cout << "You can press any key to continue or press 'q' to quit";
    again = getche();
    }while(again!='q');
    clrscr();
    cout << "Have a nice day!" << endl;
    getche();
    }
    dd

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    27
    I could tell if you used CODE TAGS.

  3. #3
    Registered User blight2c's Avatar
    Join Date
    Mar 2002
    Posts
    266
    XiReDDeViLiX, no reason to get angry, just tell him how to do it.
    [cod_e]//...[/code] just don't put the underscore in.

    sam, i think i can help (lord knows i'm not the best for the task though). i think you need to use the fstream like this:

    Code:
    char filename[20]="a:filename.*";
    	int mode=ios:: out;
    	fstream fout(filename, mode);
    sorry, that's the best i can do
    Last edited by blight2c; 04-03-2002 at 09:57 PM.

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    3
    Hi bright2c,

    regarding to the coding, is it adding the #include<fstream>, and where should i put the code

    char filename[20]="a:filename.*";
    int mode=ios:: out;
    fstream fout(filename, mode);

    Kindly enlighten me

    Thanks

  5. #5
    Registered User blight2c's Avatar
    Join Date
    Mar 2002
    Posts
    266
    sorry, i just don't know . . . not up to that chapter i suppose

  6. #6
    I'm Back
    Join Date
    Dec 2001
    Posts
    556
    after you have coded the text to whatever you want do this

    FOR WRITING IN FILE
    Code:
     ofstream fout; 
     fout.open("CODED.FIL");
     fout<<code; // to write into file
     fout.close();
    when you want to read for decoding

    FOR READING FROM FILE
    Code:
     ifstream fin; 
     fin.open("CODED.FIL");
     fin>>code; // to read from file
     fin.close();
    use #include<fstream.h> for file-handling


    You could also search the board for a lot similar questions regarding encryption/decryption, questions on filehandling etc.
    -

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Totally confused on assigment using linked lists
    By Uchihanokonoha in forum C++ Programming
    Replies: 8
    Last Post: 01-05-2008, 04:49 PM
  2. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM