I have a source file file1
and a destination file file2
, here I have to move content from file1
to file2
.
So I have to do some validation first.
I must check source file is existing or not? I can check using this:
fp = fopen( argv[1],"r" ); if ( fp == NULL ) { printf( "Could not open source file\n" ); exit(1); }
Then I have to check if the source file has any content or not? If it is empty, I have to throw some error message.
This is what I've tried until the moment.
You can do this without opening the file as well using the
stat
method.So if the file doesn't exist you'll hit the first
if
and get a message like:"No such file or directory"
If the file exists and is empty you'll get the second message
"File is empty"
This functionality exists on both Linux and Windows, but on Win it's
_stat
. I haven't tested the windows code yet, but you can see examples of it here.Just look if there's a character to read
reference for ftell and example
reference for fseek and example
you can use the
feof()
function. example:you can check if the file size > 0
after your code of checking file exist (before you close the file) you add the following code
You can use fseek using
SEEK_END
and then ftell to get the size of a file in bytes.