-
fputc problem
Greetings. I wrote next program:
Code:
#include <stdio.h>
int main(int argc, char *argv[])
{
FILE *fp, *fp2;
int c;
if(argc!=3) {
printf("Usage:program infile outfile\n");
return 0;}
else
fp=fopen(argv[1],"r");
if(fp==NULL)
{printf("Error: problem with opening %s\n",argv[1]);
return 0;}
fp2=fopen(argv[2],"w");
if(fp==NULL)
{printf("Error: problem with opening %s\n",argv[2]);
return 0;}
while(c=fgetc(fp)!=EOF)
{
fputc(c,fp2);
}
printf("Done.\n");
fclose(fp);
fclose(fp2);
return 0;
}
File _first_: "Hello, try to copy this"
After running program file _second_ looks like this:
ЪЪЪЪЪЪЪЪЪЪЪЪЪЪ ЪЪЪЪЪЪЪЪЪЪ
Help plz.
-
This:
Code:
while(c=fgetc(fp)!=EOF)
Is equivalent to:
Code:
while(c=(fgetc(fp)!=EOF))
“c” is being set to either 0 or 1. You want:
Code:
while((c=fgetc(fp))!=EOF)
-
Thanks a lot, man! It really helps me!