Hey, i am a beginner at programming but basically what im attempting right now is a account verification system. i have figured out how to verify the password for the specific account but rather than program each account into the program i want it to read it from a file so i can easily add new accounts to the system?
Anyone have any insight on how to approach this? i have searched around and haven't really found anything that i could use to solve this problem.
any advice is appreciated thanks!
Accessing data in a file?
Started by Psynic, Sep 29 2009 09:14 PM
7 replies to this topic
#1
Posted 29 September 2009 - 09:14 PM
|
|
|
#2
Posted 30 September 2009 - 06:01 AM
Uhm, are you talking about Windows user accounts?
proudly presenting my personal website and game website: F1Simulation. a thrilling Managed DirectX racing game... also my Ask Me
look at my tutorials about cropping images and Mono: bundling Mono with programs and lambda expressions
look at my tutorials about cropping images and Mono: bundling Mono with programs and lambda expressions
#3
Posted 30 September 2009 - 08:32 AM
No. what i am doing is making a console application in C# that essentially prompts you to enter an account name.. then based on that account will retrieve the password and prompt you for the password then verify you enter the correct pass.
Just like a login for anything else except it is just a console app. i am trying to achieve this by reading the account and password combination from a file rather than having to program each account into the application.
There are things like filestream i have read about but nothing really tells you how the different functions of it work i was looking for a little light to be shed on that.
Just like a login for anything else except it is just a console app. i am trying to achieve this by reading the account and password combination from a file rather than having to program each account into the application.
There are things like filestream i have read about but nothing really tells you how the different functions of it work i was looking for a little light to be shed on that.
#4
Posted 30 September 2009 - 08:56 AM
I see, just some internal database (or file) with logins and passwords. What do you need, a textfile with names and passwords?
proudly presenting my personal website and game website: F1Simulation. a thrilling Managed DirectX racing game... also my Ask Me
look at my tutorials about cropping images and Mono: bundling Mono with programs and lambda expressions
look at my tutorials about cropping images and Mono: bundling Mono with programs and lambda expressions
#5
Posted 30 September 2009 - 03:36 PM
you are a beginner, so to keep it simple:
create a file users.txt with the following format:
acount number,username,password. initially it is empty.
after a first user, named user1, registered a new acount (acount number 1) with password 1234 the text file should have the following line as it's single line
1,user1,1234
when a user tries to enter, you read the text file line by line to find the user/password/acount
example of finding the password, given the username. if not found return the empty string:
create a file users.txt with the following format:
acount number,username,password. initially it is empty.
after a first user, named user1, registered a new acount (acount number 1) with password 1234 the text file should have the following line as it's single line
1,user1,1234
when a user tries to enter, you read the text file line by line to find the user/password/acount
example of finding the password, given the username. if not found return the empty string:
private string nameOfFile = "wowowow.txt";
string FindPassword(string username)
{
string result = string.Empty;
StreamReader streamReader;
string line;
streamReader=File.OpenText(nameOfFile);
line = streamReader.ReadLine();
while(line !=null)
{
string[] details = line.split(",");
if(details[1] == username)
{
result = details[2];
line = null;
}
else
line = streamReader.ReadLine();
}
SR.Close();
return result;
}
Edited by Jaan, 01 October 2009 - 06:05 AM.
Please use code tags when you are posting your codes !
#6
Posted 30 September 2009 - 05:14 PM
Thanks auto i will try to play around with that and see what i can accomplish. maybe u should make a tutorial about reading/writing files :P
#7
Posted 30 September 2009 - 11:36 PM
so auto! i was soo pumped when it worked.. at least for my first account. but if i type in the second account name it obviously isnt details[1] so then it triggers the else statement and just reads the same line over not giving me a password for the second account is there any way that i could make it so if it triggers the else statement that it will read the next line in the file consecutively for multiple accounts? anyone know how to accomplish this? the code im using is almost identical to autoboxings code above.
#8
Posted 01 October 2009 - 05:15 AM
Psynic said:
so auto! i was soo pumped when it worked.. at least for my first account. but if i type in the second account name it obviously isnt details[1] so then it triggers the else statement and just reads the same line over not giving me a password for the second account is there any way that i could make it so if it triggers the else statement that it will read the next line in the file consecutively for multiple accounts? anyone know how to accomplish this? the code im using is almost identical to autoboxings code above.
It won't read the same line over and over. It reads the next line.
Now I wrote it in Visual studio and ran it and it worked.
Here's the code of the method.
private string nameOfFile = "wow.txt";
/**
* example of wow.txt:
* 1,user1,pass1
* 2,user2,pass2
* 3,user3,pass3
* 4,user4,pass4
* */
public string FindPassword(string username)
{
string result = string.Empty;
StreamReader streamReader = File.OpenText(nameOfFile);
string line = streamReader.ReadLine();
while (line != null)
{
string[] details = line.Split(',');
if (details[1] == username)
{
result = details[2];
line = null;
}
else
{
line = streamReader.ReadLine();
}
}
streamReader.Close();
return result;
}
Lets say you wish to find the password of user2,it reads the first line, which is the string "1,user1,pass1"
and after spliting, details[0] = "1"; details[1]="user1"; details[2]="pass1";
it checks whether details[1] == user2, obviously returning false, so
it executes the else statement.Now, line = "2,user2,pass2";
After split, details[0]="2", details[1]="user2"; details[2]="pass2";
the if statement returns true, so result = "pass2"; and line=null;
it then exits the while, closes the stream and returns "pass2";
A note, there are many tutorials about writing and reading any type of files(Text files in this case, XML files, etc) in c#, google a bit.
Edited by Jaan, 01 October 2009 - 06:06 AM.
Please use code tags when you are posting your codes !


Sign In
Create Account


Back to top









