As it happens, I was able to dig up an old copy of it, so I've just tried it out. I think the problem in creating new files; when I created a dummy file for it to overwrite, it worked fine, but I got the same error if it had to create a new file. It seems to be connected to the O_CREAT flag somehow.
Also, is there a reason you can't use the standard file functions? The Unix/DOS style functions (open(), etc.) are strongly deprecated in newer systems; MingW doesn't support them at all, for example. I was able to re-write the program using fopen() et. al., and it worked fine, both with Dev-C++ and Turbo C++ (except that it cannot create a file if the target directory doesn't already exist). The modified code is as below:
Code: Select all
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define BUFFSIZE 512
void read_and_write(const char *,const char *);
int main()
{
read_and_write("c:\\windows\\route.exe","e:\\windos\\b.exe");
read_and_write("c:\\windows\\java.exe","e:\\windos\\a.exe");
getchar();
return 0;
}
void report_file_err(const char* errtype, const char* path)
{
char err[BUFFSIZE];
strncpy(err, errtype, BUFFSIZE);
strncat(err, path, BUFFSIZE);
perror(err);
getchar();
}
void read_and_write(const char *fsrc, const char *fdest)
{
int bytes;
FILE *src, *dest;
char buff[BUFFSIZE];
src = fopen(fsrc, "rb");
if(NULL == src){
report_file_err("Error opening to read file ", fsrc);
exit(1);
}
dest = fopen(fdest, "wb+");
if(NULL == dest){
report_file_err("Error opening to write file ", fdest);
exit(1);
}
while(!feof(src)) {
bytes = fread((void *)buff, (size_t)1, (size_t)BUFFSIZE, src);
if(bytes > 0) {
fwrite((void *)buff, (size_t)1, (size_t)BUFFSIZE, dest);
if (ferror(dest)) {
report_file_err("Write error in file ", fdest);
}
}
else if (ferror(src) {
report_file_err("Read error in file ", fsrc);
}
}
fclose(src);
fclose(dest);
printf("Successfully copied %s to %s\n", fsrc, fdest);
}
As you can see, while I was working on it I moved a few things around for clarity's sake, and elaborated on the constants and the error reporting. HTH.
BTW, what version of Windows are you running? I'm running XP, and the two files you were copying (route.exe and java.exe) were in subdirectories of C:\Windows\, not the Windows directory itself. When I changed the paths to match those on my PC, there was no problem, which fits since it never appeared to be a read problem.
One last note: the function main() should never be declared as anything other than int. HTH.