Mail Archives: djgpp/1999/09/01/06:51:10
These are C language beginner's mistakes you are making here, by failing to
initialize pointers to memory areas with the memory areas you need to set
aside. The variables 'ename' and 'epass' should be either pointers to dynamic
(malloc'd) memory or to arrays of type char (better in your case). Moreover,
you should call functions that mediate input (keyboard, etc.) to these memory
areas and which allow you to control the bounds of memory so that you don't
exceed them: there are some people who don't like the use of gets() for
example and prefer fgets().
Since you are posting through an NNTP gateway, you have access to comp.lang.c,
surely, and would probably benefit from additional explanations of the
fundamentals of C language by the regulars of that newsgroup.
On 31 Aug 99, Billy was found to have commented thusly:
> #include <stdlib.h>
> #include <stdio.h>
> #include <string.h>
> #include <conio.h>
>
> int main()
> {
> FILE *infile, *outfile;
> char *ename, *cpass, *epass;
>
> clrscr();
> printf("Enter character's name:\n");
> gets(ename);
>
> infile = fopen(strcat("players/", ename), "r");
> if (!infile)
> {
> printf("New player!\n");
> printf("Enter password:\n");
> gets(epass);
> outfile = fopen(strcat("players/", ename), "w");
> fputs(epass, outfile);
> fclose(outfile);
> printf("Welcome!\n");
> }
> .....
>
> I wanted the code to create a file with the same
> filename as the player's name (in the players subdir),
> and in it I wanted the player's password.
> This is the output I get (the player "foobar" doesn't
> exist):
>
> Enter character's name:
> foobar
> oobarEnter password:
> blah
> Welcome!
>
>
> A file is created in the players/ dir
> called "foobarfoobar" with the correct password
> in it, and you see the "oobar" has replaced
> the "New player!" line that I wanted in. What
> did I do wrong?
> Thanks alot.
>
Mitch Halloran
Research (Bio)chemist
Duzen Laboratories Group
Ankara TURKEY
- Raw text -