a text encoder

I wrote this encoder program a while back. The program originally used two different text files and a different logic. the program worked fine.

I recently improved the logic to its current form. while doing that i changed program to use the just one text file.


The program wasnt working properly.Through debugging i noticed (atleast thats what i think) my compiler[ i use tc win 4.5 ] is treating the 'get' pointer and the 'put' pointer as the same. so i includes a line of code to account for that.

Now The program runs fine when the text file to be encrypted has only a few character (~20). [after the correction]
but when I i use a text file with a much larger no. of characters the program fails totally. [even after the correction]

plz advice.








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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include<fstream.h>
#include<conio.h>
#include<string.h>

class encoder
{char ch,ch1,filename[25], key;
 public:
 encoder(int);

 void encrypt()
 {cout<<">>>";               //for show
  ch1=ch^key;
  cout<<"\b\b\b";            //for show
 }

 void decrypt()
 {char a,b,c,d,e;            // temporary variables
  cout<<">>>";               //for show
  a=ch|key;
  b=ch^key;
  c=a|(~b);
  d=b&c;
  e=c&(~d);
  ch1=~e;
  cout<<"\b\b\b";            //for show
 }

};

encoder::encoder(int x)
 { cout<<"\nEnter the file name: ";
	cin.getline(filename,25);                // collecting file name
	int l=strlen(filename);
	if(filename[l-4]!='.')
	 strcat(filename,".txt");

	fstream file;
	file.open(filename,ios::in|ios::out|ios::nocreate);   //opening file 
	 if(!file)
	  cout<<"\nFile not found!!";
	 else
	 {
		cout<<"Enter KEY:";
		key=getche();               //reading encryption key
		cout<<endl;

		int p,g;      // debug
		while(1)

		{ if(file.eof()!=0)          //eof detection
				break;
		  file>>ch;              //reading from file
		  p=file.tellp();      // debug
		  g=file.tellg();      // debug
		  cout<<endl<<"g="<<g<<"\tp="<<p;      // debug
		  
                  file.seekp(-1,ios::cur);          //correction term
                      //file.seekg(-,ios::cur);    alternate correction term

		  if(x==1)
			 encrypt();
		  else
			 decrypt();
		  file<<ch1;               //writing to file
		  cout<<"\tch="<<ch<<"\tch1="<<ch1;      // debug
		}
		cout<<"\nOperation Sucessful!!";
		file.close();
	 }
 }






int choice()
{                                                              // User Menu
 char x='e';
 clrscr();
 cout<<"--------------------\n";
 cout<<"   Encoder 1.22\n";
 cout<<"--------------------\n";
 cout<<"Encrypt->e\nDecrypt->d\n";
 x=getch();
 clrscr();
 cout<<"--------------------\n";
 cout<<"   Encoder 1.22\n";
 cout<<"--------------------\n";
  if(x=='e'||x=='E')
  {cout<<" Encryption Mode\n--------------------\n";
	encoder e(1);
  }
  else
  {cout<<" Decryption Mode\n--------------------\n";
  encoder e(0);
  }
 return 0;
}



int main()
{char y='y';
 do
 {choice();
  cout<<"\nContinue ?";                            // to repeat or not to repeat
  y=getche();
 }while(y=='y'||y=='Y');
 return 0;
}




try using a newer compiler, why are you using that outdated one?

fstream.h ? that's definitely deprecated.
i'm a student and can't afford to buy a new compiler
can u suggest a free alternative??
wx dev c++ is free.
i'll dwnld that

but that will take some time
so can u any of u guys check the code for me?
and if fstream is deprecated then what header should i be useing?
i dwnloaded devc++
but i cant compile

Anything at all

i keep on getting something like

Compiler: Default GCC compiler
Building Makefile: "C:\Users\Soumya\Documents\Makefile.win"
Executing  make...
mingw32-make.exe -f "Makefile.win" all 
g++.exe -c ../Desktop/Untitled1.cpp -o Objects/MingW/Untitled1.o -I"C:/Program Files/Dev-Cpp/lib/gcc/mingw32/3.4.5/include" -I"C:/Program Files/Dev-Cpp/include" -I"C:/Program Files/Dev-Cpp/" -I"C:/Program Files/Dev-Cpp/include/3rdparty/wx/plotctrl" -I"C:/Program Files/Dev-Cpp/include/3rdparty/wx/sheet" -I"C:/Program Files/Dev-Cpp/include/3rdparty/wx/treemultictrl" -I"C:/Program Files/Dev-Cpp/include/3rdparty/wx/things" -I"C:/Program Files/Dev-Cpp/include/3rdparty/wx" -I"C:/Program Files/Dev-Cpp/include/3rdparty" -I"C:/Program Files/Dev-Cpp/include/common/wx/msw" -I"C:/Program Files/Dev-Cpp/include/common/wx/generic" -I"C:/Program Files/Dev-Cpp/include/common/wx/fl" -I"C:/Program Files/Dev-Cpp/include/common/wx/gizmos" -I"C:/Program Files/Dev-Cpp/include/common/wx/html" -I"C:/Program Files/Dev-Cpp/include/common/wx/mmedia" -I"C:/Program Files/Dev-Cpp/include/common/wx/net" -I"C:/Program Files/Dev-Cpp/include/common/wx/ogl" -I"C:/Program Files/Dev-Cpp/include/common/wx/plot" -I"C:/Program Files/Dev-Cpp/include/common/wx/protocol" -I"C:/Program Files/Dev-Cpp/include/common/wx/stc" -I"C:/Program Files/Dev-Cpp/include/common/wx/svg" -I"C:/Program Files/Dev-Cpp/include/common/wx/xml" -I"C:/Program Files/Dev-Cpp/include/common/wx/xrc" -I"C:/Program Files/Dev-Cpp/include/common/wx" -I"C:/Program Files/Dev-Cpp/include/common"    -ansi -traditional-cpp -fexceptions -fverbose-asm -g3 -fmessage-length=0

'g++.exe' is not recognized as an internal or external command,
operable program or batch file.

mingw32-make.exe: *** [Objects/MingW/Untitled1.o] Error 1

Execution terminated
Compilation Failed. Make returned 2

Topic archived. No new replies allowed.