delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/09/01/06:51:10

Message-Id: <199909010454.HAA01339@ankara.Foo.COM>
From: "S. M. Halloran" <mitch AT duzen DOT com DOT tr>
Organization: User RFC 822- and 1123-compliant
To: djgpp AT delorie DOT com
Date: Wed, 1 Sep 1999 08:59:29 +0200
MIME-Version: 1.0
Subject: Re: Code help pls
In-reply-to: <37cc35bf.1702101@news.erols.com>
X-mailer: Pegasus Mail for Win32 (v3.12)
Reply-To: djgpp AT delorie DOT com
X-Mailing-List: djgpp AT delorie DOT com
X-Unsubscribes-To: listserv AT delorie DOT com

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 -


  webmaster     delorie software   privacy  
  Copyright 2019   by DJ Delorie     Updated Jul 2019