The fuction of the codes below is to parse strings from a text file. An example of a string in the input file would be:
Entry=1
Name=Archie S. Andrews
When I compile the the following codes in either Borland C++ 4.5 or Microsoft Visual C++ 6, I get no errors. However when I try to run them I get different results. With Borland, I get a "Could not create process" message. With Microsoft I'm able to run it but then I get additional characters printed out in the 'mi' character. It prints out garbage.
I can't pinpoint what is causing the problem. Please help.
#include "stdio.h"
#include "conio.h"
#include <io.h>
#include <string.h>
#include <process.h>
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);
}
if ((fhandle_out = fopen("jenny.txt","w")) == NULL)
{
printf("cannot open file");
exit(1);
}
while (!feof(fhandle_in))
{
if (fhandle_in!=NULL)
{
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;
}
// 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
printf("$100:%s, %s",lname,mi);
}
}
}
getch();
fclose(fhandle_out);
fclose(fhandle_in);
return 0;
}
Thanks
Entry=1
Name=Archie S. Andrews
When I compile the the following codes in either Borland C++ 4.5 or Microsoft Visual C++ 6, I get no errors. However when I try to run them I get different results. With Borland, I get a "Could not create process" message. With Microsoft I'm able to run it but then I get additional characters printed out in the 'mi' character. It prints out garbage.
I can't pinpoint what is causing the problem. Please help.
#include "stdio.h"
#include "conio.h"
#include <io.h>
#include <string.h>
#include <process.h>
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);
}
if ((fhandle_out = fopen("jenny.txt","w")) == NULL)
{
printf("cannot open file");
exit(1);
}
while (!feof(fhandle_in))
{
if (fhandle_in!=NULL)
{
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;
}
// 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
printf("$100:%s, %s",lname,mi);
}
}
}
getch();
fclose(fhandle_out);
fclose(fhandle_in);
return 0;
}
Thanks