Computing.Net > Forums > Programming > C arrays of strings from a file

Computing.Net: Over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to sign up now, it's free!

C arrays of strings from a file

Reply to Message Icon

Original Message
Name: technoaddicts
Date: February 28, 2006 at 13:40:58 Pacific
Subject: C arrays of strings from a file
OS: XP
CPU/Ram: AMD 64 3200MHz 1GB DDR
Model/Manufacturer: Techno Addicts
Comment:

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


Report Offensive Message For Removal


Response Number 1
Name: Don Arnett
Date: February 28, 2006 at 18:56:18 Pacific
Reply: (edit)

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!


Report Offensive Follow Up For Removal

Response Number 2
Name: technoaddicts
Date: March 1, 2006 at 01:15:47 Pacific
Reply: (edit)

Yeah, that would be helpfull cheers.

Justice comes to he who waits


Report Offensive Follow Up For Removal

Response Number 3
Name: technoaddicts
Date: March 1, 2006 at 06:55:36 Pacific
Reply: (edit)

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


Report Offensive Follow Up For Removal

Response Number 4
Name: nails
Date: March 1, 2006 at 08:34:30 Pacific
Reply: (edit)

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


Report Offensive Follow Up For Removal

Response Number 5
Name: technoaddicts
Date: March 3, 2006 at 03:57:48 Pacific
Reply: (edit)

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


Report Offensive Follow Up For Removal


Response Number 6
Name: nails
Date: March 3, 2006 at 14:55:55 Pacific
Reply: (edit)

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?



Report Offensive Follow Up For Removal






Post Locked

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


Go to Programming Forum Home








Do you own an iPhone?

Yes
No, but soon
No


View Results

Poll Finishes In 7 Days.
Discuss in The Lounge
Poll History




Data Recovery Software