Question on writing c program implement cp function using UNIX system calls - read, write, etc.

Question on writing c program implement cp function using UNIX system calls - read, write, etc.

Post by Rob » Tue, 18 Sep 2001 07:03:47



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().

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));

Is that right? If I do this will it work for all files whether they are
binary or ascii files?

The 3rd arg of read() takes in how many  bytes to read, how do I tell it to
read in the entire source file?

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.

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.

 
 
 

Question on writing c program implement cp function using UNIX system calls - read, write, etc.

Post by jon sajda » Tue, 18 Sep 2001 09:53:09


Quote:> 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().

sounds like you're taking an OS class. :)

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?

there's nothing wrong with using a buffer, the data has to sit somewhere
between reading it and writing it.

Quote:> Well if I have to make a buffer for input
> I read in I do this:

> c = (char *) calloc(100, sizeof(char));

> Is that right? If I do this will it work for all files whether they are
> binary or ascii files?

c = (char *) malloc(sizeof(char) * 100));

both are variations on the same thing, ive always liked malloc better
than calloc personally.. it doesnt try to initialize anythnig w/o me
knowing it.

ive never had any problems using either binary or ascii in that type of
buffer.

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?

what if the source file is 100M? you dont want to attempt to read all that
into memory, you dont want to have to make a buffer capable of
accomodating that by default, and you shouldnt have to make a stretchable
buffer for such a basic system copy.

why not use a moderate buffer, and read->buffer->write continously until
the file is copied. make the bufferSize something reasonable, like 64K.

while((actualAmountRead = read(infile, buffer, bufferSize)) > 0){
  if(write(outfile, buffer, actualAmountRead) < 0) {
    perror (destinationFilename);
    break;
  }

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.

> 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.

the above should help clear up some of this. when in doubt, the man pages
are a great help too.

/jon

 
 
 

Question on writing c program implement cp function using UNIX system calls - read, write, etc.

Post by Erik Max Franci » Tue, 18 Sep 2001 13:36:56



> c = (char *) malloc(sizeof(char) * 100));

Note that in C the cast is unnecessary and can actually mask problems
(such as neglecting to #include <stdlib.h>).  In C++ a cast is necessary
and should be a static_cast.

--

 __ San Jose, CA, US / 37 20 N 121 53 W / ICQ16063900 / &tSftDotIotE
/  \ All generalizations are dangerous, even this one.
\__/ Dumas Fils
    Maths reference / http://www.alcyone.com/max/reference/maths/
 A mathematics reference.

 
 
 

1. Need help writing C program using UNIX system calls (read, write, etc) that copies files

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().

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));

Is that right? If I do this will it work for all files whether they are
binary or ascii files?

The 3rd arg of read() takes in how many  bytes to read, how do I tell it to
read in the entire source file?

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.

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.

2. SCSI Problems with FBSD3.2/Adaptec

3. Sockets: write() writes OK, yet read() reads garbage...?

4. Mounting swap space as /tmp

5. WRite system call and printf function

6. Problem with script, values not printing

7. Using COM ports with read/write OR streams with fread etc?

8. Default console font?

9. Read & Write MSR system calls

10. system call read(), write() thread-safe?

11. How to force write function to write to device immediatly

12. C function "write" cannot write more than 2 Gb

13. how to read disk read & write i/o through a c/c++ program ?