Recalling values from an array of structures

Hi, I'm trying to save the values I read from a file to an array of structures of type user_login. Although the print statements in the for loop work, the last 3 print statements in the following code don't return the respective user information.

It should read:
User 1 info is: User#1 and 123456
User 2 info is: User#2 and pass123
User 3 info is: User#3 and F4567
but reads:
User 1 info is: User#3 and F4567
User 2 info is: User#3 and F4567
User 3 info is: User#3 and F4567

Does anyone have any idea to what I'm doing wrong? Thanks in advance.
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;
}
Topic archived. No new replies allowed.