Saturday, 7 April 2018

file io - C ,fscanf(), last line is read twice

Hi I’m doing a test program in C File I/O for my System software (assemblers,loader etc ) course , my problem is the last line is read twice , I remember my teacher told me this is due to some slight syntax or bug I missed ,I forgot what it is , please take a look and help me quick.



Program




#include
#include
//read from source.txt and write to output.txt

int main()
{
FILE *f1=fopen("source.txt","r");
FILE *f2=fopen("output.txt","w");
int address;
char label[20],opcode[20];

while(!feof(f1))//feof returns 1 if end of file
{
fscanf(f1,"%s\t%s\t%d",label,opcode,&address);
printf("%s\t%s\t%d\n",label,opcode,address);
fprintf(f2,"%s\t%s\t%d\n",label,opcode,address);
}
int check=fclose(f1);
int check2=fclose(f2);
printf("close status %d %d",check,check2);
return 0;

}


source.txt



NULL    LDA     4000
ALPHA STA 5000
BETA ADD 4020// I stopped right here, DID NOT PRESS 'ENTER' , so that ain’t the issue



output.txt



NULL    LDA     4000
ALPHA STA 5000
BETA ADD 4020
BETA ADD 4020


//last line twice




output in terminal



NULL    LDA     4000
ALPHA STA 5000
BETA ADD 4020
BETA ADD 4020


//last line twice




I don't want the last line to be printed or written twice , what am I doing wrong , help!

No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...