• Advertisement

Archived

This topic is now archived and is closed to further replies.

File I/O help: space and newline

This topic is 5363 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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]

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites

  • Advertisement