Hi everyone,
I wrote a programme to copy the contents of one file into another and I agree that I wrote some useless and extra code. The programme works fine except that it adds an extra ÿ at the end of the file that it copies contents to.
Here's the code:
Code:
#include <stdio.h>
#include <unistd.h>
void fcp(FILE *fp, FILE *sfp);
int main (int argc, char *argv[])
{
FILE *fp;
FILE *sfp;
char c, h;
int x;
if (argc < 2)
{
printf("Proper Usage:\n./D20Exc5 <FILENAME1> <FILENAME2>\n");
return 0;
}
if ((fp = fopen(argv[1], "r")) == NULL)
{
printf("ERROR Opening file %s.\n", argv[1]);
return 1;
}
if ((sfp = fopen(argv[2], "w+")) == NULL)
{
printf("ERROR Opening file %s.\n", argv[2]);
return 1;
}
fcp(fp, sfp);
for (x = 0; !feof(fp); x++)
{
fseek(fp, x, SEEK_SET);
fseek(sfp,x, SEEK_SET);
c = fgetc(fp);
h = fgetc(sfp);
if (c != h)
{
puts("Failed Copying Files...");
puts("Trying Again...");
puts("Press Ctrl-C to stop at anytime.");
execl("/home/rhodium/C/Day20/D20Exc5", "fcp",
argv[1], argv[2]);
}
}
fclose(fp);
fclose(sfp);
return 0;
}
void fcp(FILE *fp, FILE *sfp)
{
char c;
while(!feof(fp))
{
c = fgetc(fp);
putc(c, sfp);
}
}
Could someone tell me why that extra character is at the end of the file and how to remove it?
Thank you