Hi
I am new here. Here I was trying to write a program to copy one file to another.
Code:
#include <stdio.h>#include <stdlib.h>
int main(int argc, char *argv[])
{
FILE *fp , *ft;
if ((fp = fopen(argv[1], "rb")) == NULL)
{
printf("Can't open %s for reading\n", argv[1]);
exit(EXIT_FAILURE);
}
if ((ft = fopen(argv[2], "wb")) == NULL)
{
printf("Can't open %s\n", argv[2]);
exit(EXIT_FAILURE);
}
int ch;
while( (feof(fp) == 0) )
{
fread(&ch, sizeof(int),1, fp);
fwrite(&ch, sizeof(int),1, ft);
}
if (ferror(fp) != 0)
fprintf(stderr,"Error in reading file %s.\n", argv[1]);
if (ferror(ft) != 0)
fprintf(stderr,"Error in writing file %s.\n", argv[2]);
fclose(ft);
fclose(fp);
return 9;
}
This program takes two filenames on the command line. e.g
.
So it should copy test1 to test2. Now I created test1 in gedit. I just put one character "c" in it and closed it. And I ran the program. When I check sizes of test1 and test2 , using command, I see that test2 has 4 bytes and test1 has 2 bytes.
So something strange is happening here. I just put a single character in test1, so shouldn't it show 1 byte ? And why test2 is showing 4 bytes ? Any help appreciated. I have Win XP on my laptop. And I use Ubuntu 12 inside VirtualBox. So this is Linux environment...
thanks
ratnakar