Jump to content
  • Advertisement


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


File I/O help: space and newline

This topic is 5545 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;


        if(password[counter] == '\0')
           counter = 0;

        ifile >> character;

        cout << character << "   ";
         character = character+password[counter];
          character = character-password[counter];
        cout << character << "\n";
        ofile << character;
    //EDIT: forgot to close my files

    //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
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
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.

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!