Reputation: 11
I'm trying to copy one test file to another in C. However my code is not working the program runs fine and produces a file CircleCode_temp but there is nothing in the text file. Any ideas why its not working
#include <stdio.h>
#include <stdlib.h>
char c;
int main(int argc, char **argv)
{
FILE *orginalFile = fopen("CircleCode1", "r");
FILE *newFile = fopen("CircleCode_temp", "w");
if (orginalFile == NULL | newFile == NULL)
{
printf("Cannot open file");
exit(0);
}
while((c = fgetc(orginalFile))!=EOF)
{
fputc(c,newFile);
c = fgetc(orginalFile);
}
fclose(orginalFile);
fclose(newFile);
return 0;
}
Upvotes: 0
Views: 40
Reputation: 182829
while((c = fgetc(orginalFile))!=EOF)
{
fputc(c,newFile);
c = fgetc(orginalFile);
}
Two mistakes here:
You call fgetc
twice in the loop, which throws every other character away.
You compare c
to EOF
. You're suppose to compare the return value of fgetc
to EOF
. If you think they're the same, remember that c
is of type char
and fgetc
returns an int
.
Also:
if (orginalFile == NULL | newFile == NULL)
One mistake here. You have |
which is bitwise OR, but you want ||
, which is a logical OR.
Upvotes: 2