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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
|
#include <cstring>
#include <string>
#include <iostream>
#include <fstream>
using namespace std;
int main () {
ifstream myfile; //input .txt file
string filename; //hardcode .txt filenames
string line; //file line in string
char c_line[150]; //file line in char
int char_length;
struct user_login {
char *name;
char *passwd;
}; //struct for user login info
user_login user[3];
filename = "UserPassMatch.txt";
myfile.open(filename.c_str());
if (myfile.is_open()){
for (int i=0; i<3; i++) {
getline (myfile, line);
line += " "; //append delimiter to line
printf("the line is: %s \n", line.c_str());
char_length = sprintf(c_line, "%s", line.c_str()); //convert string to char
user[i].name = strtok (c_line," "); //save username
printf ("username is: %s\n",user[i].name);
user[i].passwd = strtok (NULL, " "); //save user password
printf ("user password is: %s\n",user[i].passwd);
}
myfile.close();
}
else {
printf("Unable to open file\n");
}
printf("User 1 info is: %s and %s \n", user[0].name, user[0].passwd);
printf("User 2 info is: %s and %s \n", user[1].name, user[1].passwd);
printf("User 3 info is: %s and %s \n", user[2].name, user[2].passwd);
return 0;
}
|