Results 1 to 9 of 9

Thread: C open() read() then write() to copy a file

  1. #1
    Join Date
    Mar 2009
    Location
    New York
    Beans
    117
    Distro
    Ubuntu 10.04 Lucid Lynx

    C open() read() then write() to copy a file

    I know its possible as my friend has been able to copy a file using C but using the open() read() and write() C commands. I have attempted in doing the same thing to merely copy a file but it is not working. Here is what I got:
    Code:
    //i have a variable size which is an int and is the byte size of the file
    //i got the byte size of file from stat
    int fileread = open("original.txt",'r');
    void *buffer;
    buffer = malloc(sizeof(void) * size);
    
    int nread = read(fileread,buffer,size);
    
    int filewrite = open("original.txt.backup",'w');
    
    write(filewrite,buffer,size);
    
    close(filewrite);
    close(fileread);
    This is creating the file original.txt.backup with some strange permissions which I can address later but its not a copy of original.txt it is just a empty file. Most of the info I have from read write and open is from http://rabbit.eng.miami.edu/info/functions/unixio.html

    Thanks!

  2. #2
    Join Date
    Jul 2008
    Beans
    76

    Re: C open() read() then write() to copy a file

    I don't have any of my code ni front of me, but I do remember having to fflush() before closing to commit any changes currently cached to disk.

  3. #3
    Join Date
    Sep 2007
    Location
    Christchurch, New Zealand
    Beans
    1,328
    Distro
    Ubuntu

    Re: C open() read() then write() to copy a file

    Quote Originally Posted by Xender1 View Post
    Code:
    int fileread = open("original.txt",'r');
    ah you confuse two different ways of doing it.
    the open, read and write functions use a file handle and you need to open it with an integer access code like so:
    Code:
    int fileread = open("original.txt", O_RDONLY);
    otoh there are functions fopen, fread and fwrite that are perhaps slightly higher level and they use struct FILE and you open them with a string like "r" or "w" which is not a single character code but a null terminated string.
    Last edited by worksofcraft; October 16th, 2010 at 02:45 AM. Reason: corrected typos

  4. #4
    Join Date
    Nov 2009
    Beans
    1,081

    Re: C open() read() then write() to copy a file

    In addition to the O_FLAG problems, read() and write() are permitted to read and write fewer bytes than you ask them to.

  5. #5
    psusi is offline Ubuntu addict and loving it
    Join Date
    Sep 2005
    Location
    Orlando, FL
    Beans
    3,980
    Distro
    Ubuntu Development Release

    Re: C open() read() then write() to copy a file

    In addition to using the wrong argument with the wrong function, you are multiplying your size by sizeof( void ) which I think would be zero, since void means NOTHING.

  6. #6
    Join Date
    Sep 2007
    Location
    Christchurch, New Zealand
    Beans
    1,328
    Distro
    Ubuntu

    Re: C open() read() then write() to copy a file

    Quote Originally Posted by psusi View Post
    In addition to using the wrong argument with the wrong function, you are multiplying your size by sizeof( void ) which I think would be zero, since void means NOTHING.
    lol - that is true, however the new ANSI standard doesn't guarantee size of char to be one anymore so people need to use sizeof(void) to get smallest addressable unit

    If you use the C++ compiler it will tell you:
    Code:
    void.c:4: warning: invalid application of ‘sizeof’ to a void type
    but if you use the C compiler it is quite happy and says:
    Code:
    $> gcc -Wall void.c
    $> ./a.out
    sizeof(void)=1

  7. #7
    Join Date
    Mar 2009
    Location
    New York
    Beans
    117
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: C open() read() then write() to copy a file

    Ah thank you all very much for your help. Following your advice I was able to fix it perfectly! Here is what I have now:
    Code:
    //i have a variable size which is an int and is the byte size of the file
    //i got the byte size of file from stat
    int fileread = open("original.txt", O_RDONLY);
    void *buffer;
    buffer = malloc(sizeof(void) * size);
    
    int nread = read(fileread,buffer,size);
    
    int filewrite = open("original.txt.backup",O_CREAT | O_RDWR, 0644);
    
    write(filewrite,buffer,size);
    
    close(filewrite);
    close(fileread);
    0644 for rw-r--r--

    Thanks!

  8. #8
    psusi is offline Ubuntu addict and loving it
    Join Date
    Sep 2005
    Location
    Orlando, FL
    Beans
    3,980
    Distro
    Ubuntu Development Release

    Re: C open() read() then write() to copy a file

    Quote Originally Posted by worksofcraft View Post
    lol - that is true, however the new ANSI standard doesn't guarantee size of char to be one anymore so people need to use sizeof(void) to get smallest addressable unit
    Ok, but why bother multiplying by one?

  9. #9
    Join Date
    Aug 2010
    Location
    Lancs, United Kingdom
    Beans
    1,089
    Distro
    Xubuntu 14.04 Trusty Tahr

    Re: C open() read() then write() to copy a file

    Quote Originally Posted by worksofcraft View Post
    lol - that is true, however the new ANSI standard doesn't guarantee size of char to be one anymore so people need to use sizeof(void) to get smallest addressable unit
    Which issue of which standard are you referring to here?

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •