Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pointers changing values eventually causing system to crash

Status
Not open for further replies.

rouge03

Programmer
May 5, 2003
16
PH
Hello!

This is in connection with my post in Borland(Inprise): C++ Builder with the subject "Help with this code, no compile errors."

I made some changes on the code and at first it seemed that my problem was solved. However I made some changes to the code to extract some characters from the string. When the program started to print out garbage instead, I reverted to the previous changes I made wherein I got the intended results. However after a few tries I still got the garbage characters and eventually I got a blank screen everytime I tried to run the program. But still no compile errors.

If you would examine the code I put in there closely you would see that I made sure(at least I thought I did) that my pointers pointed to something relevant.

Any info on this matter will be greatly appreciated. If you want me to paste the code in this post again, i'll do it on the next log on. I didn't put it at this time because its too long.
 
I forgot to mention that I have to do a User-break (Ctrl-Pause) to back to the editor. I'm using Borland C ver 3 I think. Also after I exit Borland and return to windows, I get an unhandled exception error.

Thanks
 
Well you could have just posted the URL

You could also have used the code tags to make the code more presentable
Code:
#include "stdio.h"	/* standard header files use <> */
#include "conio.h"
#include <io.h>		/* no obvious need for this header file */
#include <string.h>
#include <process.h>	/* no obvious need for this header file */

/* main returns an int */
/* it defaults to int, but you won't have that luxury for much longer */
/* so now is the time to say what you mean */
int main()
{
 FILE *fhandle_in, *fhandle_out;
 char *ptr,buffer[80],name[80],lname[20],mi[2];
 int acc_no=0;
 clrscr();
 if ((fhandle_in = fopen("train1.txt", "r")) == NULL)
  {
   printf("cannot open file");
   exit(1); /* you also need to include <stdlib.h> */
  }
  if ((fhandle_out = fopen("jenny.txt","w")) == NULL)
   {
     printf("cannot open file");
     exit(1);
   }

   /* feof() is state, not a prediction - see later comments */
   /* have you seen the "last line twice" problem yet? */
   while (!feof(fhandle_in))
    {
    /* this is a useless test - if you get this far, fhandle_in will always be != NULL */
    if (fhandle_in!=NULL)
    {
     /* use sizeof(buffer) for better portability */
     fgets(buffer,80,fhandle_in);
     if ((ptr=strstr(buffer,"Entry="))!=NULL) //get value for Accession number
     {
         acc_no++;
         printf("$001:%010d",acc_no);
         ptr-=6; /* this serves no purpose, except to yeild an out of bound pointer */
     }

    // Name segment
    if ((ptr=strstr(buffer,"Name="))!=NULL) 
    {
      ptr+=5;
      strcpy(name,ptr);
          if (name[strlen(name)-1]=='\n')
         name[strlen(name)-1]='\0';  // remove newline character
          ptr=strrchr(name,'.');
      ptr+=2;
      strcpy(lname,ptr);
      ptr -=3;
        strncpy(mi,ptr,2); // get the middle initial
	mi[2] = '\0';  /* make sure it's a proper string */

     printf("$100:%s, %s",lname,mi);
    }
   }
}
getch();
fclose(fhandle_out);
fclose(fhandle_in);
return 0;
}
I've also included some comments to fix certain things

As regarding feof() etc, the correct way to read all the lines in a file is
Code:
while ( fgets(buffer, sizeof buffer,fhandle_in) != NULL ) {
  /* do stuff */
}

--
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top