I have written the following code
but i ´m getting an
"Bad File Descriptor"
while trying to write in Dest
Any idea ?
#include
#include
#include
#include
#include
#include
main()
{
int Source;
int Dest;
int Dest2;
char srcfile[80];
char destfile[80];
char buffer[256];
int ret ;
int c;
struct stat *filetype;
filetype = (struct stat *)(malloc(sizeof(struct stat)));
printf("File name to copy :");
gets(srcfile);
/*printf("File name to copy to :");
gets(destfile);*/
/*printf("%d\n",S_ISDIR(filetype->st_mode));*/
sprintf(destfile,"%sb",srcfile);
if ( ! (Source = open(srcfile,O_RDONLY)) )
printf ( "File Not Found\n");
else
{
Dest = open(destfile,O_CREAT);
Dest2 = open("saida",O_CREAT);
fstat(Source,filetype);
fchmod(Dest,filetype->st_mode);
buffer[0] = 0;
lseek(Source,0,SEEK_SET);
while ( c = read(Source,&buffer,128) )
{
buffer[c] = 0;
c = strlen(buffer);
puts(buffer);
write(Dest,&buffer,c);
write(Dest2,&buffer,c);
printf("write error : %s FD:%d Source : %d\n",strerror(errno),Dest,Source);
buffer[0] = 0;
}
buffer[c] = 0;
c = strlen(buffer);
write(Dest,&buffer,c);
close(Dest);
}
}