Find String in C Plus Plus

I want to make a program of Account from user give input Id No. and Password and the program check whether the id exist or not if exist verify the password input by user. The record should be in a text file as below.
Use simple way using classes as i have not expert in C++. It is best that password will be in case sensitive

File is in multiple lines with tabs between ID Pass and Income as below

Record.txt
ID Pass Income
3626 xYasd 50,000
554 89dd 100,000
6997 di2M 70,500
0125 msuu 1,500
6554 kDew 120,000
What you are saying dont make any sense!!Anyway,below there is the source code for a progam I have written which finds any string you want in any type of file.It might be useful to you.The two unknown librarys "Symbol.h" and Exception_total-time" are handmade by me.The first one is is about string manipulation and the second one determines the total time from an intial point up to an other.The algorithm is quite efficient.The program works perfect.Seeking for a "non-existed"(worst case) string in a 700MB file with Pentium 4 3.14GHz 512RAM program needs 4-5 minutes.

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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include<iostream>
#include<fstream>
#include "Symbol.h"
#include "EXecuTiON_TotAL_TiME.hpp"

using namespace std;

int STANDAR_BUF_SIZE = 4096;
int COVER_BUF        = STANDAR_BUF_SIZE;
int SMaRT_BUF        = STANDAR_BUF_SIZE;
char BuFFeR[4097];

ifstream file;

int extra_BuF(int c,int str_length,Symbol str,char* dest);

main()
{
 char filepath[52];

 cout<<"Give the filepath:";
 cin.get( filepath , 51 );

 file.open( filepath , ios::binary );
 if( !file.is_open())
 {
  cerr<<"--------------\aERROR opening the file ["<<filepath<<"]--probalby not found---"<<endl;
  cerr<<"--------------Program terminated-----------------------------------";
  exit(1);
 }
//####################################################//
 unsigned long file_size=0;
 file.seekg(0,ios::end);
 file_size=file.tellg();
 file.seekg(0,ios::beg);
 cout<<"filesize:"<<file_size<<endl;

 Symbol str;
 cout<<"Give the string to find:";
 cin>>str;
 cout<<"Distinction of lower and upper cases? yes/1  ,  no/anything else:";
 char dest[1];
 cin>>dest[0];
  cout<<"\n\n--------------PLeaSE WaiTE------------------"<<endl;
//**************************************// 
//*************************************//

 EXecuTiON_TotAL_TiME total_time;//initial point to count the time the crucial code needs to be executed

 int str_len=str.Length();
 char strbuffer[str_len+1];

 for(unsigned long int i=0; i<file_size; i+=COVER_BUF)
 {
  /*----------------------------------------*/
  if( file_size-i<STANDAR_BUF_SIZE )
  {
   SMaRT_BUF=file_size-i+1;
  }
  file.read( BuFFeR , SMaRT_BUF );
  /*----------------------------------------*/
  for(int C=0; C<SMaRT_BUF; C++)
  {
   if( SMaRT_BUF-C<str_len )
   {
    //COVER_BUF=C;
    COVER_BUF=extra_BuF(C,str_len,str,dest);
    if( file.eof() )
    {
     COVER_BUF=SMaRT_BUF;
    }
    file.seekg(COVER_BUF-SMaRT_BUF,ios::cur);
    break;
   }

   for(int c1=0,c2=C; c1<str_len; c1++,c2++)
   {
    strbuffer[c1]=BuFFeR[c2];
    strbuffer[c1+1]='\0';
   }
  
   Symbol str2( strbuffer );

   if(/****/str==str2 || (/**/(dest[0]!='1') && (str^str2)/**/)/****/)
   {
    cout<<"\a\a\a\a\astring:["<<str<<"] was found!!!----ORIGINAL STRING:["<<str2<<"]\a\a\a\a\a"<<endl;
    /*******************--time--*****************************/
    cout<<"TOTAL TIME: ";
    total_time.GetFormattedTime();
    cout<<"\n\nFPT:";
    FPT.GetFormattedTime();
    /********************************************************/
    file.close();
    system("pause");
    exit(1);
   }
  }//end of internal for loop
 }//end of external for loop

 file.close();
 cout<<"\a\astring:["<<str<<"] not found!!!\a\a"<<endl;
/*********************--total time--***************/
/*************************************************/ 
 cout<<"TOTAL TIME: ";
 total_time.GetFormattedTime();
 cout<<"\n\nFPT:";

 FPT.GetFormattedTime();

 system("pause");
}//end of main
/******************************************************************************/
/******************************************************************************/
int extra_BuF(int c,int str_length,Symbol str,char* dest)
{
 char strbuffer[str_length+1];

 for(int k=c; SMaRT_BUF-k!=0; k++)
 {
  for(int c1=0,c2=k; c1<SMaRT_BUF-k; c1++,c2++)
  {
   strbuffer[c1]=BuFFeR[c2];
   strbuffer[c1+1]='\0';
  }
  
  Symbol str2( strbuffer );
  char* tempstr=str.SymbolToChar();
  tempstr[SMaRT_BUF-k]='\0';
  str=tempstr;
 
  if(/****/str==str2 || (/**/(dest[0]!='1') && (str^str2)/**/)/****/)
  {
   return k;
  }
 }
 return STANDAR_BUF_SIZE;
}
Last edited on
The "Symbol.h" library's source code.

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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
#include<iostream>

using namespace std;

class Symbol
{
 public:
        Symbol(){buffer=new char[520];}//constructor 1
        Symbol(char *str);            //constructor 2
        Symbol(const Symbol&);        //copy constructor
        ~Symbol();
        int Length();
        char* SymbolToChar(char* ch);
        Symbol* operator=(const Symbol& x);
        Symbol* operator=(char* x);
        bool operator==(const Symbol& x);
        bool operator^(const Symbol& x);
        bool EqualRegardlessCase(const Symbol& x);
        bool operator!=(const Symbol& x);
        char operator[](int i);
        Symbol operator+(const Symbol& x); //strings concatenation
        void Input()const;
        void Output(ostream& out)const;

 private: 
         char *buffer;
};

Symbol::Symbol(char *str)//constructor 1  
{
 int length=0;
 for(char* pstr=str; pstr!='\0'; pstr++)
 {
  length++;
 }
 buffer=new char[length+1];
 char* pb=buffer;
 for(char* pstr=str; pstr!='\0'; pstr++,pb++)
 {
  *pb=(*pstr);
  *(pb+1)='\0';
 }
}

Symbol::~Symbol()
{
 delete []buffer;
 buffer=0;
}

Symbol::Symbol(const Symbol& x)//copy constructor
{ 
 int length=0;
 for(char* pxb=x.buffer; *pxb!='\0'; pxb++)
 {
  length++;
 }
 this->buffer=new char[length+1];
 char* pb=this->buffer;  
 for(char* pxb=x.buffer; *pxb!='\0'; pxb++,pb++)
 {
  *pb=(*pxb);
  *(pb+1)='\0';
 }
}
 
int Symbol::Length()
{
 int length=0;
 for(char* pb=this->buffer; *pb!='\0'; pb++)
 {
  length++;
 }
 return length;
}

char* Symbol::SymbolToChar(char* ch=0)
{
 if(ch!=0)
 {
  delete []ch;
 }
 int len=this->Length();
 char* ToCharBuffer=new char[len+1];
 char* pch=ToCharBuffer;

 for(char* pb=this->buffer; *pb!='\0'; pb++,pch++)
 {
  *pch=(*pb);
  *(pch+1)='\0';
 }
 return ToCharBuffer;
}


Symbol* Symbol::operator=(const Symbol& x)
{
 if(this==&x)
 {
  return this;
 }
 
 if( this->buffer!=0 )
 {
  delete []this->buffer;
  this->buffer=0;
 }
 int length=0;
 for(char* pxb=x.buffer; *pxb!='\0'; pxb++)
 {
  length++;
 }
 this->buffer=new char[length+1];
 char* pb=this->buffer;
 for(char*pxb=x.buffer; *pxb!='\0'; pxb++,pb++)
 {
  *pb=(*pxb);
  *(pb+1)='\0'; 
 }
 return this;
}


Symbol* Symbol::operator=(char* x)//e.g Symbol nickname="mitsos"; like char* nickname="mitsos;
{
 if( this->buffer!=0 )
 {
  delete []this->buffer;
  this->buffer=0;
 }

 int len=0;
 for(char* px=x; (*px)!='\0'; px++)
 {
  len++;  
 }
 this->buffer=new char[len+1];
 for(char* px=x,*pb=this->buffer; (*px)!='\0'; px++,pb++)
 {
  *pb=(*px);
  *(pb+1)='\0';
 }
 return this; 
}

bool Symbol::operator==(const Symbol& x)
{
 for(char* pb=this->buffer,*pxb=x.buffer; (*pb)||(*pxb)!='\0'; pb++,pxb++)
 {
  if( (*pb)!=(*pxb) )
  {
   return false;
  }
 }
 return true;
}

bool Symbol::operator^(const Symbol& x)        //A-Z: 65-90  a-z: 97-122
{
 for(char* pb=this->buffer,*pxb=x.buffer; *pb||*pxb!='\0'; pb++,pxb++)
 {
  if(/****/(*pb!=(*pxb)) && (( (*pb-*pxb)!=(32)&&(*pb-*pxb)!=(-32) )  || (*pb>122||(*pxb)>122||*pb<65||(*pxb)<65||((*pb>90&&(*pb)<97)||((*pxb)>90&&(*pxb)<97))))/****/)
  {
   return false;
  }
 }
 return true;
}


bool Symbol::EqualRegardlessCase(const Symbol& x)
{
 for(char* pb=this->buffer,*pxb=x.buffer; *pb||*pxb!='\0'; pb++,pxb++)
 {
  if(/****/(*pb!=(*pxb)) && (( (*pb-*pxb)!=(32)&&(*pb-*pxb)!=(-32) )  ||  (*pb>122||(*pxb)>122||*pb<65||(*pxb)<65||((*pb>90&&(*pb)<97)||((*pxb)>90&&(*pxb)<97))))/****/)
  {
   return false;
  }
 }
 return true;
}


bool Symbol::operator!=(const Symbol& x)
{
 for(char* pb=this->buffer,*pxb=x.buffer; (*pb)||(*pxb)!='\0'; pb++,pxb++)
 {
  if( (*pb)!=(*pxb) )
  {
   return true;
  }
 }
 return false;
}

char Symbol::operator[](int i)
{
 if( i>=0 && i<this->Length() )
 {
  return this->buffer[i];
 } 
}


Symbol Symbol::operator+(const Symbol& x)//string concatenation
{
 int lenb=0,lenxb=0;
 for(char* pb=this->buffer; *pb!='\0'; pb++)
 {
  lenb++;
 }
 for(char* pxb=x.buffer; *pxb!='\0'; pxb++)
 {
  lenxb++;
 }
 int total_len=lenb+lenxb;
 char* sb=new char[total_len+1];
 char* psb=sb;
 for(char* pb=this->buffer; *pb!='\0'; pb++,psb++)
 {
  *psb=(*pb);
 }
 for(char* pxb=x.buffer; *pxb!='\0'; pxb++,psb++)
 {
  *psb=(*pxb);
  *(psb+1)='\0';
 }
 Symbol concat(sb);
 return concat;
}

void Symbol::Input()const
{
 cin.get(buffer,510);
 cin.ignore(4096,'\n');
}


istream& operator>>(istream& in , const Symbol& x)
{
 x.Input();
 return in;
}


void Symbol::Output(ostream& out)const
{
 out<<this->buffer;  
}

ostream& operator<<(ostream& out , const Symbol& x)
{
 x.Output(out);
 return out;
}
Last edited on
The "Execution_total_time.hpp" library's source code.

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
#include<iostream>

using namespace std;

class EXecuTiON_TotAL_TiME
{
 public:
        EXecuTiON_TotAL_TiME(){ hours=minutes=seconds=0; start_seconds=time(NULL);}
        ~EXecuTiON_TotAL_TiME(){}
        short int GetHours();
        short int GetMinutes();
        short int GetSeconds();
        int GetTotalMinutes(){ return (time(NULL)-start_seconds)/60; }
        int GetTotalSeconds(){ return (time(NULL)-start_seconds); }
          voidGetFormattedTime(){cout<<GetHours()<<"(h):"<<GetMinutes()<<"(m):"<<GetSeconds()<<"(s)"<<endl;}

        

 private:
         short int hours;
         short int minutes;
         short int seconds;
         int start_seconds;
};


short int EXecuTiON_TotAL_TiME::GetHours()
{
 hours=(time(NULL)-start_seconds)/3600;     
 return hours;
}

short int EXecuTiON_TotAL_TiME::GetMinutes()
{
 minutes=((time(NULL)-start_seconds)/60)-(GetHours()*60);
 return minutes;
}

short int EXecuTiON_TotAL_TiME::GetSeconds()
{
  seconds=(time(NULL)-start_seconds)-(GetHours()*3600)-(GetMinutes()*60);    
  return seconds;    
}
/****************************************************************************************************************/
/*--an EXecuTiON_TotAL_TiME object is defined: FTP=Full Program Time from the very first command to the point defined by the user*/

EXecuTiON_TotAL_TiME FPT;

Last edited on
Topic archived. No new replies allowed.