is a string is the cause?

Hi to all, please, help me to resolve a problem with a class function, which returns me a null string instead read data from a file.
Here is the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
	string DeCrypting()
	{
		PFile.open("Password.dat");
		Password Pw;

		    PFile.seekg(0, ios::beg );

			char *SavedData = new char[Pw.MAX_SIZE * 2];
			SavedData[0] = 0;
			if(PFile.is_open())
			{
				PFile.read(SavedData, 14);
				Pw.DeCryptedPassword.clear();
				Pw.DeCryptedPassword = SavedData;
				int a;
				a = Pw.DeCryptedPassword.size();
				Pw.DeCryptedPassword.erase(14,a-14);
			}
			return Pw.DeCryptedPassword;
	};




What do you think will happen on line 17 when "a" is less than 14?

http://www.cplusplus.com/reference/string/string/erase/

Hint: you need to know the details about "size_t".
it's just a part of code. exists a check of lenght before this function is called.
If you want real help, please post all the code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
	Password Pw;
	int L;
	switch(Pw.FileCheckStatus()){//Checking if the file exists
		case true://file exists
			L = Pw.FileCheckSize();
			if (L == 0)
			{
				Pw.NewPassword();
			}
			else if(L > 0)//when file is not empty
			{
				Pw.PasswordCheck();
			}
			break;


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
void PasswordCheck()
	{
				cout<<"Password: ";
				Password Pw;
				cin>>Pw.Typed_Password;

				if ((Pw.Typed_Password.empty())|(Pw.Typed_Password.length() <Pw.MAX_SIZE)|
					(Pw.Typed_Password.length()> Pw.MAX_SIZE)){
					cerr<<"Wrong Password.\n";}
				else

					Pw.DeCrypting();
					string EPd;
					char *A = new char[Pw.MAX_SIZE * 2];
					A[0] = 0;
					for(int i = 0;i <= Pw.MAX_SIZE; i++)
						{
							char *buff = new char[2];
							sprintf(buff, "%d",Pw.Typed_Password[i]);
							A = strcat(A,buff);
						}
					EPd.clear();
					EPd = A;
					//clearing the string for obtaing purged data;
					int a;
					a = EPd.size();
					EPd.erase(14,a-14);
				if (Pw.DeCryptedPassword.compare(EPd)!=0){printf("Wrong Password.\n");}
					else cout<<"Password accepted.\n";
Last edited on
What is Pw.Typed_Password?

What is the value of EPd immediately after line 23?
Topic archived. No new replies allowed.