💾 Archived View for mirrors.apple2.org.za › archive › ground.icaen.uiowa.edu › MiscInfo › C › stdin.… captured on 2023-01-29 at 09:36:42.

View Raw

More Information

-=-=-=-=-=-=-

Path: news.weeg.uiowa.edu!news.uiowa.edu!hobbes.physics.uiowa.edu!math.ohio-state.edu!cs.utexas.edu!news.tamu.edu!tam2000!j0p7771
From: j0p7771@tam2000.tamu.edu (Jason Perez)
Newsgroups: comp.sys.apple2.programmer
Subject: reading from stdin
Date: 7 Jun 1994 08:57:59 GMT
Organization: Texas A&M University, College Station, Tx
Lines: 22
Message-ID: <2t1cqn$48e@news.tamu.edu>
NNTP-Posting-Host: tam2000.tamu.edu

   the C code below is what i'm using to read data from stdin
and place it into a single character array.  the code works,
but is there a better way to place stdin in a single array?

        char buffer[256],*teText,*tempbuf;
 
        teText = (char *) malloc(1); /* initial malloc for initial free */
        teText[0] = (char) 0;
        while (fgets(buffer,255,fin) != NULL) {  /* fin == stdin */
                tempbuf = (char *) malloc(strlen(teText) + 1);
                strcpy(tempbuf,teText); /* save what has been read in */
                free(teText);
                teText = (char *) malloc(strlen(tempbuf) + 256);
                strcpy(teText,tempbuf); /* restore previous data */
                strcat(teText,buffer);  /* add on new data */
                free(tempbuf);
        }

--
Jason Perez                     | "Frodo Lives!"       "Gig 'em!"
j0p7771@tam2000.tamu.edu        | Texas A&M Univ. - it's not just for 
jperez@ee.tamu.edu              |                   farmers anymore!