Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hi Guys,
Can someone give me a simple example of how to read a file of userids and passwords such as a file called passwords containing:
a001mm password1
cd98fk outwilldo
lmn99p abc123456
into an array of strings in c. I know they need to be arrays of arrays of chars but not sure how to put this altogether.if you could knock up a small prog that reads the above file into arrays of userid and password respectively then uses a for loop to fprint the contents of one of the arrays out, that would be excellent.
thanks in advance
Justice comes to he who waits

How about if I knock up a small program that reads the file into one array then outputs the contents to the screen using a do while loop??
Be sure to come back and let us know if our suggestions helped!

So you know what i have upto now:
a passwords.txt file with userids followed by a space then the passwords in the following format:
user0001 password01
user0002 password02
user0003 password03
#include <stdio.h>
#include <strings.h>char *id[3];
char *pw[3];int main(void)
{
FILE *pwdFile = fopen("passwords.txt", "r");
while(fscanf(pwdFile, "%s%s", id, pw) !EOF)
{
;/* want some way of incrementing id & pw as wont let me id++, pw++ in above fscanf */
}printf("%s %s\n", id, pw);
/* this prints out last item in file ok
but i obviously want to print them all
*//* this next bit causes segment error */
int count;
for(count = 0; count < 3; count++)
{
printf("%s %s\n", id[count], pw[count]);
}
return 0;
}Justice comes to he who waits

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);
}

Thanks for the reply.
It looks a bit more complicated than what i was trying but then again mine doesnt work :-)
I have typed in your code above but when i complile it with gcc i get to warnings about incompatible implicit declaration of built-in function exit.
It still compiles and does read in the file and print it out fine however but i was wondering how to get shot of the warnings?
cheers
Justice comes to he who waits

You're welcome. Interesting problem. I originally created that code on a Solaris 7 box. I moved it over to an old Red Hat 7.1 system using gcc:
Reading specs from /usr/lib/gcc-lib/i386-redhat-linux/2.96/specs
gcc version 2.96 20000731 (Red Hat Linux 7.1 2.96-81)I didn't have any problems.
What happens if you replace the exit with the return?

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |