Computing.Net > Forums > Programming > Writing a safecopy file in C - HELP!!!

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Writing a safecopy file in C - HELP!!!

Reply to Message Icon

Name: Ian Tennant
Date: March 22, 2002 at 09:57:56 Pacific
Comment:

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 help

Thanks

Ian



Sponsored Link
Ads by Google

Response Number 1
Name: johnzarp
Date: March 23, 2002 at 20:58:48 Pacific
Reply:

too many unknowns, drop me a line at johnzarp@attbi.com and I'll see if I can help you!


0

Response Number 2
Name: miranda
Date: April 1, 2002 at 04:30:09 Pacific
Reply:

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);
}
}


0

Response Number 3
Name: miranda
Date: April 1, 2002 at 08:59:27 Pacific
Reply:

I have fixed the problem
I´ll e-mail the code
I guess i have missed some tests , tough



0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Writing a safecopy file in C - HELP!!!

Writing to a file in C www.computing.net/answers/programming/writing-to-a-file-in-c/10521.html

File Descriptors in C?? www.computing.net/answers/programming/file-descriptors-in-c/10512.html

Problem reading file in c++ www.computing.net/answers/programming/problem-reading-file-in-c/7826.html