0

I have trouble searching for a solution on how to send a successfully, copied file to a chosen destination and/or directory in C, instead of copying a file into the same destination. How do you do that? what should I do to fix this problem?

Code:

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>

int main(int argc, char *file[])
{
    int ch;  //ch - character no.                               
    struct stat sb;
    FILE *source, *target;
    /* from http://stackoverflow.com/questions/30215462/how-to-get-the-source-  files-the-file-which-i-want-to-copy-and-the-copied-file/30217023#30217023 */
    if (argc <= 2) {
        printf("Enter source and destination file names\n");
        exit(EXIT_FAILURE);
    }

    if ( strcmp(file[1], file[2]) ==0 )
    {
        printf("the files are the same\n");
        exit(1);
    }
    /* if ( file[2] != NULL)
    {
        printf("the destination file exists\n");
        exit(1);
    }*/
    source = fopen(file[1], "r");//getting and opening source file
    if( source == NULL ) {
        printf("Press any key to exit...\n");
        exit(EXIT_FAILURE);
    }

    target = fopen(file[2], "w");//getting and opening destination file

    if( target == NULL ) {
        fclose(source);
        printf("Press any key to exit...\n");
        exit(EXIT_FAILURE);
    }

    while( ( ch = fgetc(source) ) != EOF )
        fputc(ch, target);

    fclose(source);
    fclose(target);
    printf("File copied successfully.\n");
    /* from http://stackoverflow.com/questions/30215462/how-to-get-the-source-files-the-file-which-i-want-to-copy-and-the-copied-file/30217023#30217023 */
    if (stat(file[1], &sb) == -1) {
        perror("stat");
        exit(1); //exit(EXIT_SUCCESS);
    }
    else
    if (chmod(file[2], sb.st_mode & 07777))//http://stackoverflow.com/questions/18032574/how-can-i-copy-permissions-from-a-file-that-already-exists
    {
        perror("chmod");
    }

    printf("Source File: %s, Inode number: %d, Mode: 0x%04X\n", file[1], (unsigned)sb.st_ino, (unsigned)sb.st_mode);

    if (stat(file[2], &sb) == -1) {
        perror("stat");
        exit(1);
    }

    char *str;
    str = (char *) malloc(15);
    strcpy(str, file[2]);

    if (stat(file[2], &sb) == -1) {//http://stackoverflow.com/questions/7430248/creating-a-new-directory-in-c
        mkdir(file[2], 0700);
    }
    printf("Destination File: %s, inode number: %d, Address = %u, Mode: 0x%04X\n", file[2], (unsigned)sb.st_ino, str, (unsigned)sb.st_mode);

    free(str);
    return 0;
}
user4728257
  • 25
  • 10
  • Can you describe the problem? File is not copied? Rights are not set? – Mathieu Apr 27 '16 at 09:07
  • the file is copied into the same location, but I want to send the copied file into a chosen, different destination instead of the copying a file into the same location as the original file. – user4728257 Apr 27 '16 at 09:21
  • 2
    How are you calling your program? As coded, all you need to do is give it the full path name of the target file and the source file should be copied there. And you need to check the return value from `fputc()` along with `fclose()` on the target file. – Andrew Henle Apr 27 '16 at 09:32
  • I am able to copy files eg. ./a.out 1.c 2.c successfully, if 2.c does not exist. – user4728257 May 03 '16 at 09:52

0 Answers0