DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C++ (http://www.daniweb.com/forums/forum8.html)
-   -   Question, basic encrpytion? (http://www.daniweb.com/forums/thread80266.html)

MrMan2787 Jun 6th, 2007 11:26 pm
Question, basic encrpytion?
 
i am trying to write a program that takes information from a file and reads it and then does some kind of encryption, simple as adding one to it

I have it reading the file,
but how would i go about readings the characters as int's
and then adding a number to them?

example:
"Hi blah blah" = 234234 345634634 345345345 345345345
I want this to be stored in their number form + some number.

Ancient Dragon Jun 6th, 2007 11:30 pm
Re: Question, basic encrpytion?
 
char data type is a small int whose value is between -126 and 127. So do do what you want just loop through the array and add 1 to the char value.
char str[] = "Hi blah blah";
// add 1 to the first character, syntax is idental to int
++str[0];

MrMan2787 Jun 6th, 2007 11:39 pm
Re: Question, basic encrpytion?
 
Thanx, hmm but i still have one problem...

I need to read the text from a file.

So is there a way to read from the file and
then put that data into an array?

Ancient Dragon Jun 6th, 2007 11:48 pm
Re: Question, basic encrpytion?
 
after reading the string from a text file to the encryption like I posted earlier. Do you know how to read files ? In c++ use the ifstream class to read a text file. Here is an example of how to read/write text files.

MrMan2787 Jun 6th, 2007 11:52 pm
Re: Question, basic encrpytion?
 
This is the code i have so far, which only displays "file.txt" and displays it.


#include<iostream>
#include<fstream>

using namespace std;

const int SIZEMAX = 81;

void read(fstream &);

int main()
{
   
      fstream dataFile;
   
      dataFile.open("file.txt", ios::in);
        if(dataFile.fail())
    {
      cout << "The file does not exist. Please check your file."
      <<    endl;

          return 0;
    }


        read(dataFile);

        dataFile.close();
      cout << endl << "Done" << endl;


    return 0;

}


      void read(fstream &file)
{
        char line[SIZEMAX];
   
          while( file >> line)
          {
              cout << line << endl;
          }
 }
p.s. is there a way to tab in this chat box?

John A Jun 7th, 2007 12:24 am
Re: Question, basic encrpytion?
 
Well, this isn't really encryption, but I can't really figure out what you're trying to do to obtain the numbers. This just prints out the ASCII value for each character by casting to int.

int i = 0;
while(myString[i]) {
    std::cout << static_cast<int>(myString[i]) << " ";
    i++;
}
std::cout << std::endl;
This code is fairly clear; it is trivial to modify it to fit into your read() function.

Ancient Dragon Jun 7th, 2007 7:12 am
Re: Question, basic encrpytion?
 
at line 42 add another loop to encrypt the line as suggested earlier then output the result to the screen as illustrated by joe.


All times are GMT -4. The time now is 11:23 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC