-
reading from file
hi,
I save a string value to a file, Now I want use this value again, but null return
save function
Code:
int save_pass(void)
{
/* function variables */
FILE *fp ;
int i ;
/* function process*/
fp = fopen("pass", "wb");
if(!fp)
{
return 1;
}
if(&newPassword)
{
fwrite(&newPassword, sizeof(string), 1, fp);
}
return 0;
}
comparing two strings
Code:
if (password == newPassword){
cout << "corect";
}
else {
cout << "Invalid";
}
load function
Code:
int load_pass(void)
{
/* function variables */
FILE *fp ;
/* function process*/
fp = fopen("pass", "rb");
if(!fp)
{
return 1;
}
fread(&newPassword, sizeof(string), 1, fp);
if(feof (fp))
{
return 0;
}
}
-
If you're using std::string, you just can't point to it and use a low-level function like fwrite.
All you'll get say is the pointer to the actual string, and maybe the allocated size of the string buffer.
Use iostream and something like
cout << password;
to begin with, to make sure it works.
-
Now I'm using these
Code:
int save_pass(string opass)
{
ofstream outClientFile( "pass.dat", ios::out );
if (!outClientFile)
{
cerr << "File could not be created" << endl;
return 1;
exit (1);
}
outClientFile << opass;
return 0;
}
string load_pass(void)
{
string ipass;
ifstream inClientFile( "pass.dat", ios::in );
if (!inClientFile)
{
cerr << "File could not be Opened" << endl;
exit (1);
}
inClientFile >> ipass;
return ipass;
}
Code:
newPassword = load_pass();
but one problem, how could I write in file binnery? if I open pass.dat I will see the password
-
Writing it out in binary won't make it magically unreadable.
You have to do that yourself with some kind of encryption and decryption.
Say
char temp[100];
strcpy( temp, opass.c_str() );
// do something to temp, then fwrite it to a file.
Something being anything you like.
Start simple, say adding 1 to each char.