Here is something from my personal telnet/ftp program.
//user_file.txt
Code:
username:password
fun:in the bright sun
whatDOyouCall:youselfINtheMirror
I've replaced my NetworkClient.send() with std::out so you can compile this.
the return value is 0 if the user fails to loging.
anything else is the User ID to the system.
The format of the User_File.txt determins which user gets what userid.
basicly which line number their user:pass appars on.
Code:
#include <iostream>
#include <fstream>
#include <vector>
struct login{
char name[26], pass[26];
};
int loginuser(std::string filepath){
std::fstream User_File;
std::vector<login> User_List;
User_File.open("user_file.txt", std::ios::in);
if(User_File.is_open()){
login temp;
//populate the user list.
do{
memset(&temp, 0, sizeof(temp));
User_File.getline(&temp.name[0], sizeof(temp.name), ':');
User_File.getline(&temp.pass[0], sizeof(temp.pass));
User_List.push_back(temp);
if(User_File.bad()){
User_List.clear();
throw( std::string("File read error") );
}
if(User_File.eof()){
User_List.pop_back(); //Do this to remove the blank entry at the end of the list.
break;
}
}while(1);
//get the terminal account incormation
std::cout << "Enter your Username: ";
std::cin.getline(&temp.name[0], sizeof(temp.name));
std::cout << "Enter your Password: ";
std::cin.getline(&temp.pass[0], sizeof(temp.pass));
//if the terminal user entered nothing.
if( !strlen(temp.name) || !strlen(temp.pass) ){
std::cout << "User cancled login" << std::endl;
return 0;
}
for(int x = User_List.size();x; x--){
if( strcmp(temp.name, User_List[x-1].name) == 0){
if( strcmp(temp.pass, User_List[x-1].pass) == 0)
return x; //We found a winner
}
}
}else{
throw( std::string("User File could not be opened") );
}
return 0;
}