File to file copy in C/C++

File to file copy in C/C++

Post by Saurabh Moha » Thu, 30 Mar 2000 04:00:00



Hi all,
        can i copy data from file1 to file2 without copying it into an
array in my code first?? (Something like memcpy?? )

Thanks,
-SM.

 
 
 

File to file copy in C/C++

Post by Arto V. Viitan » Fri, 31 Mar 2000 04:00:00


 Saurabh> Hi all, can i copy data from file1 to file2 without copying it into
 Saurabh> an array in my code first?? (Something like memcpy?? )

You could use mmmap(). That is, you map the contents of the files into the
programs memory space. Then you could use memcpy() to copy contents of the
first file to the second.

Check program 12.14 from W. Richard Stevens "Advanced Programming in the UNIX
Environment", Addison-Wesley, 1992. The book's example programs are at
ftp://ftp.uu.net/.vol/1/published/books/stevens.advprog.tar.Z .

--

University of Tampere, Department of Computer and Information Sciences
Tampere, Finland                                      http://www.cs.uta.fi/~av/

 
 
 

File to file copy in C/C++

Post by Andrew Gabri » Fri, 31 Mar 2000 04:00:00





>  Saurabh> Hi all, can i copy data from file1 to file2 without copying it into
>  Saurabh> an array in my code first?? (Something like memcpy?? )

> You could use mmmap(). That is, you map the contents of the files into the
> programs memory space. Then you could use memcpy() to copy contents of the
> first file to the second.

Just mmap the source file, and pass the mmap address to a write call on
a fd to the target file. That way, you've lost the extra copy.

--
Andrew Gabriel
Consultant Software Engineer

 
 
 

File to file copy in C/C++

Post by K Stah » Fri, 31 Mar 2000 04:00:00



> Hi all,
>         can i copy data from file1 to file2 without copying it into an
> array in my code first?? (Something like memcpy?? )

> Thanks,
> -SM.

I don't recommend this due to performance/efficiency concerns, but it
technically answers your question:

#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

static const char OutfileName[] = {"/tmp/tmpfile"};

int main(int argc, char **argv)
{
    char InfileName[80];
    int Ifile = 0;
    int Ofile = 0;
    ssize_t Nbytes = 0;
    ssize_t Result = 0;
    char buff[1] = {0};

    if (argc > 1)
        sprintf(InfileName,"%s",argv[1]);
    else{
        fprintf(stderr,"No source file name specified\n");
        return(1);
    }

    if ((Ifile = open(InfileName,O_RDONLY)) < 0){
        fprintf(stderr,
                "Cannot open file %s for reading. %s\n",
                InfileName,
                strerror(errno));
        return(1);
    }

    if (fchmod(Ifile,(mode_t) S_IRUSR|S_IWUSR) < 0){
        fprintf(stderr,
                "Unable to set permissions for  %s. %s\n",
                OutfileName,
                strerror(errno));
        return(1);
    }

    if ((Ofile = open(OutfileName,O_WRONLY|O_SYNC|O_CREAT|O_TRUNC)) < 0){
        fprintf(stderr,
                "Cannot open file %s for writing. %s\n",
                OutfileName,
                strerror(errno));
        return(1);
    }

    if (fchmod(Ofile,(mode_t) S_IRUSR|S_IWUSR) < 0){
        fprintf(stderr,
                "Unable to set permissions for  %s. %s\n",
                OutfileName,
                strerror(errno));
        return(1);
    }

    while ((Nbytes = read(Ifile,(void *) buff,(size_t) sizeof(buff))) !=
0){
        if (Nbytes < 0){
            fprintf(stderr,
                    "Error while writing to %s. %s\n",
                    OutfileName,
                    strerror(errno));
            return(1);
        }
        else if ((Result = write(Ofile,(void *) buff,Nbytes)) < 0){
            fprintf(stderr,
                    "Error while writing to %s. %s\n",
                    OutfileName,
                    strerror(errno));
            return(1);
        }
        else
            continue;
    }
    close(Ifile);
    close(Ofile);
    return(0);

Quote:}