read() and buffer size errors in (C Linux)
<p>//EDIT: I set the first handle's flag as O_WRONLY, it should be O_RDONLY and that was causing a problem.</p>

<p>I'm working on a simple program in Linux using C that would copy text from one file to another.</p>

<pre><code>#include&lt;fcntl.h&gt;
#include&lt;unistd.h&gt;
#include&lt;stdio.h&gt;

...

int main(int argc, char * argv[])
{
    int cp_from = open(argv[1],O_WRONLY);
    int cp_to = open(argv[2],O_WRONLY|O_CREAT,0777);    //make a descriptor for each file
    int size = lseek(cp_from,0,SEEK_END);               //check the size of the first file
    lseek(cp_from,0,SEEK_SET)                           //return to the start of the file, don't know if that's needed
    char *buf = (char*) malloc (size*sizeof(char));     //allocate enough memory to fit all text from 1st file into char array
    read(cp_from,buf,sizeof(buf)-1);                    //read from the 1st file to the char array
    printf("%s",buf);                                   //print the buf
    close(cp_from);
    close(cp_to);
...
</code></pre>

<p>So, later I would write() the "buf" to "cp_to" and that would (hopefully) work. But, here's only half of the work because it stopped working at this point, "buf" is empty and I don't know why. Any ideas? </p>