[comp.sys.amiga.programmer] Problems with string handling...

gt1619a@prism.gatech.EDU (Net Runner Mark V) (02/18/91)

I have been having a problem in writing a file parser (which reads in a file
which contains a database of information and separates and translates the info
into data files for another suite of programs).

Problem: system crashes when I do this. It gets as far as the string copy,
but falls apart at the string concatenation . I'm using Manx.

The code:
- . -
- . -
char type[2], *seqName, *tblName, *string;
- . -
fscanf(input," %s;",string);
strcpy(seqName,string);
strcat(seqName,".seq");
- . -
- . -


I have also tried:
   seqName = strcat(seqName,".seq");

which also causes the damned thing to crash.


Any ideas?
-Thanx.

-------------------------------------------------------------------------
James D. McIninch
-------------------------------------------------------------------------
School of Applied Biology
Georgia Institute of Technology, Box 31619
Atlanta, Georgia 30332
uucp:     ...!{decvax,hplabs,ncar,purdue,rutgers}!gatech!prism!gt1619a
Internet: gt1619a@prism.gatech.edu
**************************************************************************
*   The goal: to design CAD/CAM software and hardware for the creation   *
*             of living things...                                        *
**************************************************************************

lkoop@pnet01.cts.com (Lamonte Koop) (02/18/91)

gt1619a@prism.gatech.EDU (Net Runner Mark V) writes:
>I have been having a problem in writing a file parser (which reads in a file
>which contains a database of information and separates and translates the info
>into data files for another suite of programs).
>
>Problem: system crashes when I do this. It gets as far as the string copy,
>but falls apart at the string concatenation . I'm using Manx.
>
>The code:
>- . -
>- . -
>char type[2], *seqName, *tblName, *string;
>- . -
>fscanf(input," %s;",string);
>strcpy(seqName,string);
>strcat(seqName,".seq");

Ok, but first thing....have you allocated memory for the pointers you declared
above?  As this code is right now (assuming no other declarations), *seqName,
*tblName, and *string are NULL.  In fact, I'm surprised your code doesn't have
problems at the fscanf() function, as this will put the scanned input to
whatever *string points at (location 0).  This should work, if this is the
case:

char type[2], seqName[50], tblName[25], string[100];

fscanf(input," %s'",string);
strcpy(seqName,string);
strcat(seqName,".seq");

Note that I have used arbitrary array size declarations.  If you want to have
better control on the size of the char arrays, then simply declare them as you
did, using malloc() to get a chunk of memory of a size suitable for your need
for each pointer.

>-------------------------------------------------------------------------
>James D. McIninch
>-------------------------------------------------------------------------
>School of Applied Biology
>Georgia Institute of Technology, Box 31619
>Atlanta, Georgia 30332
>uucp:     ...!{decvax,hplabs,ncar,purdue,rutgers}!gatech!prism!gt1619a
>Internet: gt1619a@prism.gatech.edu
>**************************************************************************
>*   The goal: to design CAD/CAM software and hardware for the creation   *
>*             of living things...                                        *
>**************************************************************************


                             LaMonte Koop
 Internet: lkoop@pnet01.cts.com         ARPA: crash!pnet01!lkoop@nosc.mil
           UUCP: {hplabs!hp-sdd ucsd nosc}!crash!pnet01!lkoop
        "Yes, I am their leader...now tell me where they went!"

markv@kuhub.cc.ukans.edu (02/19/91)

In article <22209@hydra.gatech.EDU>, gt1619a@prism.gatech.EDU (Net Runner Mark V) writes:
> I have been having a problem in writing a file parser (which reads in a file
>which contains a database of information and separates and translates the info
> into data files for another suite of programs).
> 
> Problem: system crashes when I do this. It gets as far as the string copy,
> but falls apart at the string concatenation . I'm using Manx.
> 
> The code:
> - . -
> - . -
> char type[2], *seqName, *tblName, *string;
> - . -
> fscanf(input," %s;",string);
> strcpy(seqName,string);
> strcat(seqName,".seq");

This may be a dumb question, but the incomplete nature of your example
requires it.  Assuming seqName et. al are as declared, you have a
boffo no-no here.  Declaring char *foo only allocates space for a
pointer, not the string.  The string goes whereever the pointer
points.  In this case it scribbles over whatever happens to be pointed
at by the pointer.  Also, it is a bad idea to use unbounded string
operations with such things unless you are absolutly sure of your data
because you can easily run past the end of the string.

Try this out for safe C string code:

#define		STRSIZE (100+1)	/* whatever size, plus one for trailng \0 */

 char type[2], seqName[STRSIZE], tblName[STRSIZE], string[STRSIZE];
 - . -
 fscanf(input, "%" #STRSIZE "s;", string); /* neat use of "stringizing" */

 strncpy(seqName, string, STRSIZE);
 strncat(seqName,".seq", STRSIZE);

Note the problem with fscanf() is that you cant pass width specifiers
as arguments like you can with printf() family.  So if you want run
time limits you have to sprintf() the format string with the correct
args.

If you only need to read a string consider fgets() instead.

> 
> I have also tried:
>    seqName = strcat(seqName,".seq");

This is superfulous in this case.
 
> which also causes the damned thing to crash.
 
Unless I am missing something your problem is you are scribbling on
memory with your string operations.  If the pointers are static data
you are scribbling on the exeception vector table (assuming no VBR),
or if auto, random memory locations.
 
> Any ideas?
> -Thanx.

This is a common C error, and a deadly on on most machines.
-- 
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Mark Gooderum			Only...		\    Good Cheer !!!
Academic Computing Services	       ///	  \___________________________
University of Kansas		     ///  /|         __    _
Bix:	  mgooderum	      \\\  ///  /__| |\/| | | _   /_\  makes it
Bitnet:   MARKV@UKANVAX		\/\/  /    | |  | | |__| /   \ possible...
Internet: markv@kuhub.cc.ukans.edu
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

rich@documail.UUCP (Rich McCallister) (02/21/91)

In article <22209@hydra.gatech.EDU>, gt1619a@prism.gatech.EDU (Net Runner Mark V) writes:
> Problem: system crashes when I do this. It gets as far as the string copy,
> but falls apart at the string concatenation . I'm using Manx.
> 
> char type[2], *seqName, *tblName, *string;
> - . -
> fscanf(input," %s;",string);
> strcpy(seqName,string);
> strcat(seqName,".seq");

   If you are using malloc() or AllocMem() or some such thing to allocate
memory for 'string' and 'seqName' before using them; and if input size (the
characters after the leading blanks in the input and before the semicolon)
are GUARANTEED to fit into the allocated space; and if 'string' is not left
unfilled by fscanf (which would allow the possiblity of 'string' pointing
to a memory area which contained several hundred bytes before the first NULL);
then I see nothing obvious.