The problem is you are declaring an array of character pointers, but you aren't initializing them to anything. I created a structure and made sure the user and password strings are larger than anything in passwords.txt:
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <stdio.h>
struct demo_struct
{
char id[10];
char pw[20];
};
int main(int argc, char *argv[])
{
FILE *fp;
char retstr[BUFSIZ];
char *f1, *f2, *p;
int max_cnt=5;
int i, cnt=0;
struct demo_struct usr[5];
if ((fp=fopen("passwords.txt", "r")) == NULL)
{
printf("ERROR: can not open passwords.txt file:\n");
exit(1);
}
while (fgets (retstr, BUFSIZ, fp) != NULL)
{
if ((p = strchr(retstr, '\n')) != NULL)
*p = '\0';
if(cnt == max_cnt)
break;
f1=strtok(retstr, " ");
f2=strtok(NULL, " ");
strcpy(usr[cnt].id, f1);
strcpy(usr[cnt].pw, f2);
cnt++;
}
for(i=0; i < cnt; i++)
printf("%s %s\n", usr[i].id, usr[i].pw);
fclose(fp);
exit(0);
}