| TIP: Click on subject to list as thread! | ANSI |
| echo: | |
|---|---|
| to: | |
| from: | |
| date: | |
| subject: | charasters arrays that ar |
DC> ...At start of program I initialize the array to be safe..
A good practice. I did so as well in the following example.
DC> ... the function that checks ... and retrieves ...
Doesn't look too bad. I took the liberty of concocting a
sample of a similar function. Test it out once.
You will need to supply a file name on the command line.
#include
#include
#include
char *aryPhrase[30] = {NULL};
int get_phrases(char *szPhrase)
{
int iIterator, iPhrase;
char buf[80], *status;
FILE *fpPhrase = fopen(szPhrase,"r");
if(!fpPhrase)
{
printf("Fatal Error! : Cannot find phrase-dictionary file."
"\n\nEnsure that %s is in your game directory.", Phrasefile);
exit(2);
}
for (iIterator = iPhrase = 0; iIterator < 30; iIterator++)
{
status = fgets(buf, 80, fpPhrase);
if(status)
{
if(aryPhrase[iPhrase])
free(aryPhrase[iPhrase]);
aryPhrase[iPhrase] = malloc(strlen(buf) + 1);
strcpy(aryPhrase[iPhrase], buf);
iPhrase++;
}
/* If I were not going to immediately reassign the pointer,
I would set it equal to NULL to avoid later confusion. */
else
break;
}
fclose(fpPhrase);
return iPhrase;
/* function returns number of phrases it actually read */
}
int main (int argc, char **argv)
{
int iterator, PhrzLimit = get_phrases(argv[1]);
for(iterator = 0; iterator < PhrzLimit; iterator++)
{
printf(aryPhrase[iterator]);
free(aryPhrase[iterator]);
aryPhrase[iterator] = NULL;
}
return 0;
}
> ] * Origin: *YOPS ]I[* 8.4 GIG * RA/FD/FE * Milwaukee, WI (1:154/750)SEEN-BY: 396/1 632/0 371 633/260 267 270 371 634/397 635/506 728 810 639/252 SEEN-BY: 670/218 @PATH: 154/750 222 396/1 633/260 635/506 728 633/267 |
|
| SOURCE: echomail via fidonet.ozzmosis.com | |
Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.