c - Why are directory files not read using fread? -
I was going through an example from Maurice Baque's Unix Book. He writes a simple copy program as outlined below. Although it fails when the input file is a directory file, I stumbled upon opendir
and some other such APIs - should I use it?
If a binary file can work with this, then why is the directory file considered different? Unix is not everything in the form of a file, even if it is being interpreted by this program.
How can I increase this program to support a directory file and then create a Maconod? I want to test this, suppose I am in / home / user1
and a $ / Copy / home / user user-home-clone
and mknod
to see how this directory will be different from home, I think the user-home-clone
does not have any reference to itself, but all other files will be in the / home / user
[even if it is a user called a file -hock-clone / home / user) because when we copy Used to command, so was not there?
#include & lt; Stdlib.h & gt; # Include & lt; Stdio.h & gt; # Include & lt; Fcntl.h & gt; # Include & lt; Unistd.h & gt; Four buffer [2048]; Int copy (file * source, file * destination) {int count; While ((count = fread (buffer, 1, size buffer, source)) gt; 0) {fwrite (buffer, 1, count, destination); } Return 0; } Int main (int argc, char * argv []) {int position; File * source; FILE * destination; If (argc! = 3) {printf ("% s takes exactly 3 arguments \ n", argv [0]); Exit (1); } Source = FOPN (RGR [1], "R"); If (source == faucet) {printf ("Can not open to read% s \ n", argv [1]); Exit (1); } Destination = FOPN (argv [2], "wb"); If (destination == faucet) {printf ("Can not open to write% s \ n", argv [2]); Exit (1); } If (copy (source, destination) == 0) {status = 0; } And {status = 1; } Fclose (source); Fclose (destination); Exit (status); }
I use the Santos 6.5 Linux Ext4 file system
Comments
Post a Comment