Hello,

i am trying to write a C program to copy a file from one directory and copy that to another directory. I am new to file handling and dont know how to proceed further. could you please guide me how to implement the property in the c code to copy a file.


Thanks

Recommended Answers

All 11 Replies

Open the original file for reading. Open the new file for writing. Then in a loop read a block of data from the input file and write it back out to the output file. Since we don't know whether the original file was write in text or binary mode its always safest to assume binary mode. And I like to read blocks of data in chuncks of 255 bytes, but you can make it almost anything you want.

open input file in binary mode
open output file in binary mode
loop
   read a block of data from input file
   write the block to output file
end of loop
close both files
commented: His views helps me a lot +1

hi Man,
Thank you for your reply.
Could you please give me some sample codes or links containing that.
Also the requirement of my project is that i have to check the contents of the file first and copy the contents of a file which is present in some other directory.

You give it a try yourself, then post back with the code you have written if you have more questions. Yes you may have to do some studying about FILE* and associated functions which I can not do for you. Also google around for c file handline tutorials to help you out with your research. Nothing teaches you better than when you research and discover the solutions to these things yourself.

is it possible to copy a zip file and exe file from one directory to another using c program?

is it possible to copy a zip file and exe file from one directory to another using c program?

Yes.

do we have any specific commands to perform that?
could you please guide me with a sample code...

Thanks

I guess you want us to write the program for you. Ain't going to happen in this lifetime. Show us that you have made an effort and posted the code you have written.

i didnt ask for a program... i asked whether there is any specific method to copy a exe file or zip file.

Just how many times do I have to tell you how to do it. I answered that question three days ago in this thread (here)

i didnt ask for a program... i asked whether there is any specific method to copy a exe file or zip file.

No.

#include <stdio.h>
#include <stdlib.h>

int main(int argn, char * argv[]) {

    int src_fd, dst_fd, n, err;
    unsigned char buffer[4096];
    char * src_path, dst_path;

    // Assume that the program takes two arguments the source path followed
    // by the destination path.

    if (argn != 3) {
        printf("Wrong argument count.\n");
        exit(1);
    }

    src_path = argv[1];
    dst_path = argv[2];

    src_fd = open(src_path );
    dst_fd = open(dst_path );

    while (1) {
        err = read(src_fd, buffer, 4096);
        if (err == -1) {
            printf("Error reading file.\n");
            exit(1);
        }
        n = err;

        if (n == 0) break;

        err = write(dst_fd, buffer, n);
        if (err == -1) {
            printf("Error writing to file.\n");
            exit(1);
        }
    }

    close(src_fd);
    close(dst_fd);
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.