How to write a C Program to Make a copy of file given in argv[1] to file given in argv[2] in C Programming Language ?
You have to use program with two arguments, the file names copy_from copy_to.
This C Program Make a copy of file given in argv[1] to file given in argv[2].
There is an access function with which we can see whether the file exists or not
access(filename,F_OK);
open 1. parameter= file name
open 2. parameter for what we want to use the file
O_RDONLY=only for reading,O_WRONLY-only for writing
There is errno variable in which there is the number of error --
if (errno!=0) there is an error
The three parameter long version of open - it can create the file if it doesnt exist
there is a creat function as well - creat(filename,permissions);
O_TRUNC = if it existed, clear the content,
O_CREAT=create if it doesn't exist
3. parameter the permission, if it has to be created
S_IRUSR=permission for reading by the owner e.g.
Read gives back the number of bytes
1. parameter the file descriptor
2. parameter the address of variable, we read into
3. parameter the number of bytes we want to read in
Write gives back the number of written bytes
1. parameter the file descriptor
2. parameter the address of variable we want to write out
3. parameter the number of bytes we want to write out
Solution:
- //make a copy of file given in argv[1] to file given in argv[2]
- #include <stdio.h>
- #include <stdlib.h>
- #include <fcntl.h> //open,creat
- #include <sys/types.h> //open
- #include <sys/stat.h>
- #include <errno.h> //perror, errno
- int main(int argc,char** argv){
- if (argc!=3){perror("You have to use program with two arguments, the file names copy_from copy_to");exit(1);}
- int f,g;
- f=open(argv[1],O_RDONLY);
- //there is an access function with which we can see whether the file exists or not
- //access(filename,F_OK);
- //open 1. parameter= file name
- //open 2. parameter for what we want to use the file
- //O_RDONLY=only for reading,O_WRONLY-only for writing
- if (f<0){ perror("Error at opening the file\n");exit(1);}
- //There is errno variable in which there is the number of error --
- //if (errno!=0) there is an error
- g=open(argv[2],O_WRONLY|O_CREAT|O_TRUNC,S_IRUSR|S_IWUSR);
- //the three parameter long version of open - it can create the file if it doesnt exist
- //there is a creat function as well - creat(filename,permissions);
- //O_TRUNC = if it existed, clear the content,
- //O_CREAT=create if it doesn't exist
- //3. parameter the permission, if it has to be created
- //S_IRUSR=permission for reading by the owner e.g.
- if (g<0){ perror("Error at opening the file\n");exit(1);}
- char c;
- while (read(f,&c,sizeof(c))){
- //read gives back the number of bytes
- //1. parameter the file descriptor
- //2. parameter the address of variable, we read into
- //3. parameter the number of bytes we want to read in
- printf("%c",c); //we prints out the content of the file on the screen
- if (write(g,&c,sizeof(c))!=sizeof(c))
- {perror("There is a mistake in writing\n");exit(1);}
- //write gives back the number of written bytes
- //1. parameter the file descriptor
- //2. parameter the address of variable we want to write out
- //3. parameter the number of bytes we want to write out
- }
- close(f);
- close(g);
- return 0;
- }
Learn More :
File
- COPY DATA FROM ONE FILE TO ANOTHER FILE USING C PROGRAM
- C Program File Input & Output Example
- C Program To Destruc Self Execution File ?
- C Program To Store Students Record In A Text File
- C Program To Write Data In A File And Search Data From File
- C Program to copy a file to another file
- C Function to read instructions from the file and obey them
- Client/Server C program to make client send the name of a file
- A small I/O bound program to copy N bytes from an input file to an output file.
- C Program to Read and Write FIFO
- File Number Control C Program Example
- C Program to opens and reads dictionary file specified in spellrc
- C Program to Recursively converts all file-names to lower-case
- C Program to Find the Size of File using File Handling Function
- C Program Recursive function that searches for a given file in a given folder
- C Program Read a char & print next char ( file to file )
- C Program to Input Student Details into a File For Two Subjects
- Identifies the architecture Windows PE (exe or dll) file was compiled C Program
- File Handling (console to file) in C Program
Copy
- COPY DATA FROM ONE FILE TO ANOTHER FILE USING C PROGRAM
- C Program To Copy One Character Array Into Another
- C Program/Code to Copies a BMP piece by piece
- C Program to copy a file to another file
- A small I/O bound program to copy N bytes from an input file to an output file.
- C Program to Create a copy of an image
- C Program Copies a BMP piece by piece
- Menu driven program in C to Calculate Length, Copy into Another Compare Concatenate of Two String
Given in argv