reading from a file

hello guys
i am having trouble reading this file even with ios::binary
can someone help me?

E

E		O ]AXK[B\]of;H	E
H

O
	LM 	0]&F 	MH 
H	

<	HE!%!/2LH&#(*R_.  +.?L&, ff)]L>
]L 
	O

O [L>
E<
H > 	E
OO<
<KOL<
E ]
O	L[L O
 L
 f


the trouble is some of the characters are skipped in the middle

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <iostream>
#include <fstream>
#include <cstring>
#include <string>
#include <cstdlib>

using namespace std;


class CRYPT
{
    string in;
    string key;
public:
    CRYPT()
    {
        cout << endl << "Enter the Key(8 max) : " ;

        {
            char tkey[9];
            cin.get(tkey,8,'\n');
            key=tkey;
        }
        cin.ignore(255,'\n');
    }
    void encode(ifstream &fin, ofstream &fout)
    {
        while(!fin.eof())
        {
            {
                char tin[17]= {0};
                fin.get(tin,16,EOF);
                in=tin;
            }
            for(unsigned int j=0,k=0; j < in.length(); k++,j++)
            {
                if(k==key.length())k=0;
                in[j]^=key[k];
            }
            cout << in;
            fout << in;
        }
    }
};

int main()
{
    CRYPT code;

    ifstream fin("README.txt");
    ofstream fout("README1.txt",ios::binary);

    if(!fin) cout << "\n\fail open 1\n";

    code.encode(fin,fout);
    fin.clear();
    fout.clear();
    fin.close();
    fout.close();

    fin.open("README1.txt",ios::binary);
    fout.open("README2.txt");
    if(!fin) cout << "\n\fail open 2\n";
    cout << endl;

    code.encode(fin,fout);
    fin.clear();
    fout.clear();
    fin.close();
    fout.close();
    system("notepad readme1.txt");
}

Last edited on
The first thought I have is this should look like this...
1
2
3
4
5

       char tin[17]= {0}; /// need to make one bigger than what you read in.  0-15 used and 16 is null.
       fin.get(tin,16,EOF);
       in=tin;
 


I think this will help, but not sure from what I read. the other thing that might be happening is note pad might not be showing you all the characters of the zeros in the text. I would have to write my own display routine for compensate for the nulls or char = 0
Last edited on
thanks for pointing out.
but the problem is still there :(
anyone?? just a tiny hint might be enough...
Line 53 and Line 63 bother me. If the files fail to open then there is no reason to continue the program. Please make a few changes:
1
2
3
4
5
6
7
8
9
10
11
12
13
/*ADD A DESTRUCTOR TO YOUR OBJECT TO HOLD THE APPLICATION OPEN*/
~CRYPT()
{
   std::cin.sync();
   std::cin.ignore();
}

/*CHANGE FILE INPUT CHECKS TO RETURN IF THE FILE DID NOT OPEN*/
if(!fin)
{
   "\nfail open\n";
   return 1;
}


EDIT: My first though about your OR encryption attempt is "Garbage In, Garbage Out". Where is your origional Text?
Last edited on
well here is the program with these changes made
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <iostream>
#include <fstream>
#include <cstring>
#include <cstdlib>
#include <string>

using namespace std;


class CRYPT
{
    string in;
    string key;
public:
    CRYPT()
    {
        cout << endl << "Enter the Key(8 max) : " ;

        {
            char tkey[9];
            cin.get(tkey,8,'\n');
            key=tkey;
        }
        cin.ignore(255,'\n');
    }
    ~CRYPT()
    {
        std::cin.sync();
        std::cin.ignore();
    }
    void encode(ifstream &fin, ofstream &fout)
    {
        while(!fin.eof())
        {
            {
                char tin[9]= {0};
                fin.get(tin,8,EOF);
                in=tin;
            }
            for(unsigned int j=0,k=0; j < in.length(); k++,j++)
            {
                if(k==key.length())k=0;
                in[j]^=key[k];
            }
            cout << in;//<<endl;
            fout << in;
        }
    }
};

int main()
{
    cout << "====================\n"
         << "Simple File Crypt\n"
         << "====================\n\n";
    CRYPT code;

    ifstream fin("README.txt");
    ofstream fout("README1.txt",ios::binary);

    if(!fin)
    {
        cout << "\n\fail open 1\n";
        return 1;
    }

    code.encode(fin,fout);
    fin.clear();
    fout.clear();
    fin.close();
    fout.close();

    fin.open("README1.txt",ios::binary);
    fout.open("README2.txt");
    if(!fin)
    {
        cout << "\n\fail open 2\n";
        return 2;
    }
    cout << endl;

    code.encode(fin,fout);

    fin.clear();
    fout.clear();
    fin.close();
    fout.close();
    system("notepad README2.txt");
}



the source text file is
AppWizard has created this simple file crypt application for you.

This file contains a summary of what you will find in each of the files that
make up your simple file crypt application.


simple file crypt.vcproj
    This is the main project file for VC++ projects generated using an Application Wizard.
    It contains information about the version of Visual C++ that generated the file, and
    information about the platforms, configurations, and project features selected with the
    Application Wizard.

simple file crypt.cpp
    This is the main application source file.



the encrypted output is
(it was pointed out that all the characters cannot be seen and copied from notepad....
is there any other method)
)&;
LE]	

HH[ 
H		L
<L
<	
<HO
Kff; E

E
<E
L&OH
HL	HHE	
 EL<
E 	H <f	
E>O
]L& L
 H&E	& Abob[ H 
H
<A<]eHEHE8E[L<
EO<]	H		L	]H3/GDH&

	[H	

 L
L
H$<& 	
L8	]BeHEHE%<O

E
O	<L  H	
E
O>
 O+NCE<E 		
L<
E 	CHfLOHE
O	<L  H& 


@O


DE	L

 <O 	>
E  	 E E
	fOHEH$	H2
Fob[ H 
H
<A&<oLLOH1 LE
	L	E
< E
>
E 	Ab



and the decrypted output is
AppWizard has created this simpile crypt application for you.

Ts file contains a summary of what you will find in eacof the fie up your simpl crypt apption.


simpl firypt.vcproj
    This is the main project file for VC++ projects generatng an Appion Wizard.
    It contains information about t version of Visua+ that ged the find
    information about t prms, configurations, and projatures sd witthe
    Application Wizard.

simpl firypt.cpp
    Tthe main applicatisource fi



whats garbage in garbage out
Last edited on
As for "another method" this is a valuable tool for a number of reasons including stuff like this: http://www.vim.org/download.php

"Garbage in garbage out" means that the guts of what ever you are doing (in this case your program) might be functioning %100, but if the input is nonsensical or somehow flawed then you won't get the results you are looking for.
here is the encoded file copied from vim after converting to hex
the input i have used here is not garbage....


0000000: 2915 1c3b 0612 041a 014c 040e 1b45 0b17  )..;.....L...E..
0000010: 090d 1b0d 0148 1104 051c 4816 0108 1c00  .....H....H.....
0000020: 0a48 0301 0909 4c0c 1a1c 1811 4c0d 1f18  .H....L.....L...
0000030: 0901 060d 1806 070b 4803 031e 4f11 0a1d  ........H...O...
0000040: 4b66 663b 000c 1b45 0a05 030d 450b 0a02  Kff;...E....E...
0000050: 180e 010b 1b45 0d4c 1c1d 0805 041e 154f  .....E.L.......O
0000060: 0703 4812 040d 1b48 1c07 104c 1b06 0409  ..H....H...L....
0000070: 4803 0502 0b48 0c06 4509 0d0c 0045 0703  H....H..E....E..
0000080: 4c18 070d 450e 0c00 091c 4811 0004 1866  L...E.....H....f
0000090: 0209 0e0d 4519 1c4f 110a 1d17 4c1f 0605  ....E..O....L...
00000a0: 1504 004c 0a06 0400 4806 1e15 1f1c 4509  ...L....H.....E.
00000b0: 151c 0006 0b04 1c0c 0302 4162 6f62 1605  ..........Abob..
00000c0: 011f 0400 4803 0500 0a48 061a 1c1c 1841  ....H....H.....A
00000d0: 1e06 1817 0306 6548 4548 4538 0406 1b45  ......eHEHE8...E
00000e0: 0116 4c18 070d 4505 0405 024f 1817 070f  ..L...E....O....
00000f0: 090f 1b48 0301 0909 4c09 0717 4833 2f47  ...H....L...H3/G
0000100: 4448 151a 0a06 090c 1c16 4802 0902 0a1a  DH........H.....
0000110: 041c 0008 4c1a 1b0c 0602 4c0d 0148 2418  ....L.....L..H$.
0000120: 1500 050c 0911 010a 024c 3801 1f09 1708  .........L8.....
0000130: 4265 4845 4845 2518 4f0b 0a06 110d 0501  BeHEHE%.O.......
0000140: 1b45 010b 0a03 1d05 041c 0c03 024f 0907  .E...........O..
0000150: 0710 184c 1b00 0048 1309 1e1c 010a 0645  ...L...H.......E
0000160: 030a 4f3e 0c1b 100d 004f 2b4e 4345 1804  ..O>.....O+NCE..
0000170: 0e1c 450f 0002 091d 0911 0d01 4c18 070d  ..E.........L...
0000180: 450e 0c00 0943 4804 0601 664c 4f48 4501  E....CH...fLOHE.
0000190: 0b0a 031d 0504 1c0c 0302 4f09 0707 1018  ..........O.....
00001a0: 4c1b 0000 4815 000d 1b0e 0a1a 081f 404f  L...H.........@O
00001b0: 0b0a 0603 050b 1a1a 041c 0c03 021c 4445  ..............DE
00001c0: 090b 084c 1f1a 0a02 000f 184f 0e00 0911  ...L.......O....
00001d0: 191e 0a1b 451b 0000 090c 1c00 0c45 1b05  ....E........E..
00001e0: 1b00 451c 0d09 664f 4845 4824 1c1c 0301  ..E...fOHEH$....
00001f0: 0609 1105 0301 4832 011f 0d1e 0b46 6f62  ......H2.....Fob
0000200: 1605 011f 0400 4803 0500 0a48 061a 1c1c  ......H....H....
0000210: 1841 0b15 186f 4c4c 4f48 3100 0c1f 4c06  .A...oLLOH1...L.
0000220: 1b45 1c0d 094c 0209 0c06 450d 1c1f 040c  .E...L....E.....
0000230: 0b04 1805 0006 451b 0a19 1e0c 0d45 0e0c  ......E......E..
0000240: 0009 4162 0a                             ..Ab.
Last edited on
this is hopeless... no one's replying
Topic archived. No new replies allowed.