>I need to write a c program that will perform like the cp command in UNIX
>and copy one file to another. I need to use the UNIX system calls read(),
>write(), open(), close(). I know how to open the source file for read access
>and destination file for write access with open(), but I am unsure of how to
>use read() and write().
You need two things at a minimum. One is a tutorial on C
programming. Here is the URL to the comp.lang.c newsgroup's FAQ
list where you can find information on both printed and online
tutorials, plus of course the FAQ itself is a great help. See
section 18.9 for a list of online tutorials and section 18.10
for a list of books.
http://www.eskimo.com/~scs/C-faq/top.html
(Note that since the functions you wish to use are _not_ part of
the Standard C language, you do NOT want to post questions about
them to comp.lang.c; instead you will definitely want to
continue this thread in this group, or in other groups more
specific to whatever variation of UNIX you are using.)
The other item you need to do is read the man pages for the
particular functions you mentioned above. It appears that you
are asking for about a homework assignment, so don't expect
anyone to actually do the work for you. But if you check out
these various sources of information and then ask specific
questions you will no doubt get very good answers. You might
consider posting sections of code too, but try to make it a
complete, compilable, and very concise program that demonstrates
your problem with the least number of lines of code possible.
Quote:>I know read takes my file descriptor (fd) of the source file I opened as its
>1st arguement. For the second, I need to put those bytes in some sort of
>buffer right? Could I just write them directly to the destination file
>instead of storing into a buffer? Well if I have to make a buffer for input
>I read in I do this:
>c = (char *) calloc(100, sizeof(char));
Assuming that c is defined as a pointer to type char, yes. But...
this is better:
#include <stdio.h>
#include <stdlib.h>
int
foo(void)
{
char *p;
if ((p = malloc(100)) == NULL) {
fprintf(stderr, "Out of memory.\n");
exit(EXIT_FAILURE);
}
...
Quote:}
Note that there is no need to cast the results of malloc(), and calloc()
is not needed, use malloc() instead. But you also must have a prototype
for malloc() in scope, which will be provided by including the stdlib.h
header file. The stdio.h header provides a prototype for fprintf(), and
definitions for the NULL and EXIT_FAILURE macros.
Also, (sizeof char) is defined as 1 by the C language, hence there is
never any reason to use it instead of just a 1.
Or, there is probably no reason to even use dynamically allocated
memory, and instead you could use a local char array instead:
int
foo(void)
{
char buffer[100];
...
Quote:}
>Is that right? If I do this will it work for all files whether they are
>binary or ascii files?
Yes.
Quote:>The 3rd arg of read() takes in how many bytes to read, how do I tell it to
>read in the entire source file?
The only way to do that would be to use a value known to be larger than
the file. Probably not a good idea.
Quote:>So for read, this is what I have so far: read(fd, c, ????) where fd is my
>source file descriptor and c as define above.
Well, you allocated 100 bytes to the buffer above, hence you would not
want to read more than 100 bytes with read() because read() do just that,
and put those bytes into memory right after the 100 that you own... and
most likely your program will crash.
Quote:>As for write, I have: write(fd2, ???, ???) where fd2 is the file descriptor
>of my destination file. How do I specify it to write what I read from the
>input source file and write all of those bytes inputted? Thanks for any
>help.
Welll... (you'll kick yourself) if you read the bytes into the
memory pointed to by c, you probably want to write those same
bytes out, eh? So point write to that same memory. And you
will need one more variable in order to tell write how many
bytes to write. Check the man page on read() to see what it
returns. Then when you call the read() function you want to
save the return value into a variable, which you can check to
see if it indicates a read error or not, and if not... use that
variable to tell the write() function how many bytes to write.
Be sure to check the return value of write() too, because it
will indicate if it succeeded or not.
--
Floyd L. Davidson <http://www.ptialaska.net/~floyd>