Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I am absoultely usless when it comes to programming in C and have been given this question
Can anyone PLEASE HELP ME!!!
I have to write a safecopy program safecp.c with the syntax: safecp source target
target may be a filename or a directory name; if it is a directory, then the name of the target file is target/source
A copy is safe if and only if the source exists and is a regular file and has read permission.
The target file does not exist or if it exists, is a regular file and has write permissions and has a modification date earlier than source
Also I am not allowed to use the shell command to do the program and I can't use command in C that have the effect to do a system call such as the command "system"
Can anyone please helpThanks
Ian

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
#includemain()
{
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);
}
}

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |