TIP: Click on subject to list as thread! ANSI
echo: aust_c_here
to: All
from: Luke Spry
date: 1995-09-20 17:06:00
subject: Big Problem

Hi all,

I'm having a big problem with a routine I've written. This routine reads a
text file into memory, One line at a time. This routine works fine for most 
files but with some files the computer just goes haywire. (One time, the 
CMOS was erased, The printer printed, The modem clicked, and the floppy disk
drive lights turned on !!!).

I think it might have something to do with unallocated memory, but I can't
seem to fathom the error.

Below is the buggy function. I'll repost the whole program if neccesary.

>- Cut Here ----------------------------------------------------------------

#define BUFLEN 500
#define MAXLINES 13000
char* text[MAXLINES];

int loadFile(char* fileName)
   {
   FILE* textFile;
   char buffer[BUFLEN];
   char *buffPnt;   
   int done = -1;   
   int tabCount;    
   /* Open the file; If not exist signal error */

   if ((textFile = fopen(fileName,"rb")) == NULL)
      {
      return(-1);
      }
   do
      {

      lineNum++;
      buffPnt = buffer; 
      
      /* read character from file into buffer until   or EOF*/
      do
         {
          if (feof(textFile)) break; 
         *buffPnt = fgetc(textFile);
         
         /* if it's a line feed ignore it */
         if (*buffPnt == '\r') buffPnt--;

         buffPnt++;
         }
      while (*(buffPnt - 1)  != '\n');

      if (feof(textFile))
         {
         done = 0;
         break;
         }

      *(buffPnt-1) = '\0';

      /* allocate length of string in buffer+1 to current string*/
      text[lineNum] = (char*)malloc(strlen(buffer)+1);
      
      if (text[lineNum] == NULL)
         {
         return(MEM_PROBS);
         }

      /* copy the string */
      strcpy(text[lineNum],buffer);

      if (lineNum == MAXLINES-1)
         {
         done = 0;
         }
      /* if it is the End-Of-File, or max lines exit the loop */


      } while (done == -1 );
      lineNum--;
      fclose(textFile);
   }

--- GEcho/32 1.20/beta+
* Origin: Caught in the ACT BBS - Canberra ACT - (06) 292-8288 (3:620/252)
SEEN-BY: 50/99 620/202 243 250 252 623/630 711/401 409 410 430 510 807 808
SEEN-BY: 711/809 932 934 712/515 713/888 714/906 800/1 7877/2809
@PATH: 620/252 243 711/808 809 934

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™.