-
file manipulation
I am trying to write a program (internal assessment for those who know anything about IB) and I'm not sure how to read from files created.
I have succesfully created a file which will hold user names/passwords and am able to add on the end with no problem. I just need to know how to check the file to see if the username exists when entered and if the password is the same as when first entered.
any help would be greatly appreciated, even links to tutorials on the internet of how to do this with examples.
Thanks,
HB9/KC5ZFZ
-
You can't manipulate data in the file while it is in the file. You have to read the file data into your program, manipulate it, then write it back to the file.
Therefore:
declare a container to hold data from file:
search container for name entered;
if name found compare password to listed;
write container contents back to file if they were changed.
If you are using c_style strings then you will need to use strcmp() or one of the related functions. If you are using a string class that has an overloaded == operator (eg STL string class), then you can do direct comparison of strings.
Note: if your file is written using standard field sizes you mignt be able to write just the changed data rather than the entire container, but that is a bit more sophisticated than writing the entire contents of the contaier.
-
i guess...
hmm i guess you could use <fstream.h>
int main();
{
ifstream import_file("blah.txt"); //replace blah.txt with your file
cout << "Enter Username";
cin.getline(user_input);
import_file >> a;
while (check_username(a) == false && import_file !EOF)
{
import_file >> a;
}
cout << "User name and password found";
getch();
return 0;
}
//--------------FUNCTIONS----------\\
bool check_username(AnsiString a);
{
if( strcomp(user_input, a) = 0)
{
return true;
}
else
{
return false;
}
}
i dunno i wrote this up in like 5 secs without thinking.. don't know if this what your looking for or if this can help.. i'm trying to figure out file input myself....this only matches usernames but you could easily add code to match usernames and passwords...(probably small syntax errors in my code, i'm too lazy to read it)
Plus i'm a begginner so beware :)