make use of fopen function to open a file, which is in stdio.h library.
FILE *fopen(const char *filename, const char *mode);
Stay connected...!
comment box is waiting for your opinion...!
FILE *fopen(const char *filename, const char *mode);
mode Description
"r" Open a file for reading. The file must exist.
"w" Create an empty file for writing. If a file with the same name already exists its content is erased and the file is considered as a new empty file.
"a" Append to a file. Writing operations append data at the end of the file. The file is created if it does not exist.
"r+" Open a file for update both reading and writing. The file must exist.
"w+" Create an empty file for both reading and writing.
"a+" Open a file for reading and appending.
"r" Open a file for reading. The file must exist.
"w" Create an empty file for writing. If a file with the same name already exists its content is erased and the file is considered as a new empty file.
"a" Append to a file. Writing operations append data at the end of the file. The file is created if it does not exist.
"r+" Open a file for update both reading and writing. The file must exist.
"w+" Create an empty file for both reading and writing.
"a+" Open a file for reading and appending.
/*
author: ba-programmer.blogspot.com
program:- coping content of one file to another file in c
*/
#includeint main() { char ch, filename[20]; FILE *source, *dest; printf("Enter name of file to copy : "); scanf(" %s",filename);//opening file in read mode source = fopen(filename, "r"); if( source == NULL ) { printf("FileNotFound"); exit(0); } printf("Enter name of file to which content should be copied : "); scanf(" %s",filename); dest = fopen(filename, "w");//opening file in write mode if( dest == NULL ) { fclose(source); printf("Error try again"); exit(0); } while( ( ch = fgetc(source) ) != EOF ) fputc(ch, dest);//reading from source and writing to destination file printf("File copy Operation successfully.\n"); fclose(source); fclose(dest);//closing files return 0; }
comment box is waiting for your opinion...!
ty bro :)
ReplyDeleteno problem Sahil Kumar its our duty....! :)
Delete