please i have this copy file program which i need too be converted into see to be used in Unix C. it works in Unix C with the g++ compiling but i need it in C because i have other functions that are in C and need to call the functions in my made shell. this is the code below;
#include <stdio.h>
#include <string.h>
#include <unistd.h>
int main()
{
// character arrays to hold the strings
// FILENAME_MAX: http://www.cplusplus.com/reference/clibrary/cstdio/FILENAME_MAX/char src_file[FILENAME_MAX];
char dest_file[FILENAME_MAX];
// write string to stdout
printf("Enter the file name to copy: ");
// request user input, into src_file variable
fgets(src_file, FILENAME_MAX, stdin);
// remove the new line character from the end of the string
src_file[ strlen(src_file) - 1 ] = '\0';
// create a FILE variable
// declare it to open the previously inputted file
// "rb" - read as binary
FILE* fin = fopen(src_file, "rb");
// if an error occured reading the file
if( fin == NULL )
{
// print the message "File error" with the error generated
perror("File error");
// exit program
return -1;
}
// once again write to stdout & request user input
printf("Enter the destination file name: ");
fgets(dest_file, FILENAME_MAX, stdin);
dest_file[ strlen(dest_file) - 1 ] = '\0';
// check if the file exists
if( !access(dest_file, F_OK) )
{
// prompt the user to over-write if it does
printf("The file '%s' already exists.\nDo you want to over-write this file (y/n)? ", dest_file);
// get input choice
char choice;
choice=getchar();
if( choice != 'y' && choice != 'Y' )
{
// if they do not want to over-write, return
return -1;
}
}
// open the file for writing
// "wb" - write as binary
FILE* fout = fopen(dest_file, "wb");
char c;
// loop through all the input characters
while( (c = fgetc(fin)) != EOF )
{
// write them to the output file
fputc( (int)c, fout );
}
// close input & output files
fclose( fin );
fclose( fout );
puts("\nDone.");
return 0;
}