Hi,
Please review the C program below and see if you can figure out what's wrong. I've been trying for a day now and I Just can't seem to get it to work.
The aim of the program is to copy the contents of one file (in.txt) to another (out.txt) using a 256B buffer.
if in.txt contains 4 Chars (eg.ABCD) then out.txt will contain these chars followed by the char 'ÿ' then 252 null characters.
in.txtout.txt
Been cracking my head trying to figure this one out.
Code:
#include <stdio.h>
#include <stdlib.h>
#define SOURCE "c:\\dev\\in.txt"
#define DEST "c:\\dev\\out.txt"
#define BUFF_SIZE 256
signed int buffer[BUFF_SIZE];
int
main (int argc, char *argv[]){
FILE *in, *out;
signed int pos, ch;
if((in = fopen(SOURCE, "r")) == NULL){
fprintf(stderr, "Error: Could not open source file\n");
exit(1);
}
if((out = fopen(DEST, "w")) == NULL){
fprintf(stderr, "Error: Could not open destination file\n");
exit(1);
}
while(!feof(in)){
pos = 0;
while (((buffer[pos] = getc(in)) != EOF) && (pos < BUFF_SIZE)){
pos++;
}
pos = 0;
while (((ch = putc(buffer[pos], out)) != EOF) && (pos < BUFF_SIZE)){
pos++;
}
}
if (fclose(in) == EOF){
fprintf(stderr, "Error: Failed to close input file");
}
if (fclose(out) == EOF){
fprintf(stderr, "Error: Failed to close output file");
}
return 0;
}