I am trying to read characters from a file and writing them to another. The problem is, though everything is being written, a weird symbol is getting appended in the next line of write file. My code is:
#include<iostream>
#include<stdlib.h>
#include<string.h>
#include<stdio.h>
using namespace std;
int main(){
FILE *f, *g;
int ch;
f = fopen("readfile", "r");
g = fopen("writefile", "w");
while(ch != EOF){
ch = getc(f);
putc(ch, g);
}
fclose(f);
fclose(g);
return 0;
}
What may be the reason for that?
It’s because you write
chto the other file before you check if it’sEOF, so that one gets written too.