rtz87
rtz87

Reputation: 235

c program to copy media file from one location to another

#include<stdio.h>
#include<stat.h>
#include<fcntl.h>
main()
{
int inhandle,outhandle,bytes;
char source[128],target[128],buffer[512];
printf("enter source file name\n");
scanf("%s",source);

inhandle=open(source,O_RDONLY|O_BINARY);
if(inhandle==-1)
{
    printf("cannot open source file\n");
    exit(0);
}
printf("enter target file name\n");
scanf("%s",target);
outhandle=open(target,O_CREAT|O_BINARY,O_WRONLY,S_IWRITE);
if(outhandle==-1)
{

    printf("cannot open target file\n");
    close(outhandle);
    exit(0);
}
while(1)
{
    bytes=read(inhandle,buffer,512);
    if(bytes>0)
    {
        write(outhandle,buffer,bytes);
    }
    else
    break;
}
close(inhandle);
close(outhandle);
}

program compiles with 0 errors and when i pass the arguments in scanf there even no errors related to opening the file is thrown.i cannot seem to copy any media file like .avi format with this program,the file does gets created in its target location but with 0 bytes.

Upvotes: 0

Views: 872

Answers (1)

cnicutar
cnicutar

Reputation: 182744

The problem is in your second open(2) call:

outhandle=open(target,O_CREAT|O_BINARY,O_WRONLY,S_IWRITE);
                                      ^        ^

Instead of the second comma, you probably meant a |. Because of that comma O_WRONLY will be the third argument, the mode and the file won't have the correct permissions.

Upvotes: 2

Related Questions