File I/O help: space and newline

Started by
1 comment, last by googlyeyes 20 years, 8 months ago
here is my program

//#include <iostream.h> included with fstream.h

#include <fstream>

//this takes a file and encrypts/decrypts it with the given password


int main()
{
    ifstream ifile;
    ofstream ofile;


    char character;
    char password[64];
    char inputfile[100];
    char outputfile[100];
    int counter = 0;
    bool state;//encrypt = 0 decrypt=1


    cout << "enter the password\n";
    cin >> password;

    cout << "\nenter the file you want to read from\n";
    cin >> inputfile;

    cout << "\nenter the file you want to write to\n";
    cin >> outputfile;

    cout << "\ndo you want to\nencrypt: 0\ndecrypt: 1\n";
    cin >> state;

    ifile.open(inputfile);
    ofile.open(outputfile);

    while(!ifile.eof())
    {
        if(password[counter] == '\0')
        {
           counter = 0;
        }

        ifile >> character;

        cout << character << "   ";
        if(!state)
        {
         character = character+password[counter];
        }
        else
        {
          character = character-password[counter];
        }
        cout << character << "\n";
        ofile << character;
        counter++;
    }
    //EDIT: forgot to close my files

    ofile.close();
    ifile.close();
    //END EDIT

    cin >> password; //wait before stopping program

    return 0;
}
the program works fine but it doesn't seem to detect spaces or newlines. also it adds an extra byte at the end of the file. KTHXBYE [edited by - googlyeyes on August 18, 2003 3:02:45 PM]
Advertisement
EDIT: Scrap what I said. You can use std::istream::get(), i.e. in your case ifile.get(). If you want to know why, std::istream's overloaded right-shift operator (>>) skips over whitespaces, whilst std::istream's get() method does not.

Incidentally, as you are using C++, you really should be using std::string. I would strongly recommend using the STL.

Bear in mind that, as it is at the moment, it is extremely easy to work out your encryption/decryption algorithm, i.e. decipher (decode).

[ Google || Start Here || ACCU || STL || Boost || MSDN || GotW || MSVC++ Library Fixes || BarrysWorld || E-Mail Me ]

[edited by - Lektrix on August 18, 2003 4:12:44 PM]
[ Google || Start Here || ACCU || STL || Boost || MSDN || GotW || CUJ || MSVC++ Library Fixes || BarrysWorld || [email=lektrix@barrysworld.com]E-Mail Me[/email] ]
thanks for the help.

I know its really easy to decypher, this is just for fun, I won''t be sending anything like social security numbers with it.

This topic is closed to new replies.

Advertisement