ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
How to copy a regular file in C using open(), read() and write()?
It seems i have program creating the new file using open().
How can i make the permission to be the same with the old file?
so far i have the following code in my function
working with just copying the content first
but i dont know why the new file is still empty after i run this
is there any problem in "write(dest_file,buffer,size);"?
Code:
void copy_file(char *source, char *dest)
{
int source_file, dest_file;
char buffer[128];
struct stat buf;
if ( lstat(source,&buf)==0 ) {
if ( !(buf.st_mode & S_IFREG) )
printf("%s: is not a regular file\n",source);
}
else
printf("%s: file does not exist\n",source);
source_file = open(source, O_RDONLY);
if ( source_file!=0 ) {
printf("%s: error in opening file\n", source);
return;
}
dest_file = open(dest, O_CREAT, S_IRWXU);
if ( dest_file!=0 ) {
printf("%s: error in creating file\n", dest);
return;
}
int size;
while( (size=read(source_file, buffer, 128)) > 0 ) {
write(dest_file,buffer,size);
}
close(source_file);
close(dest_file);
return;
}
That's odd. open should return unique file descriptors; your tests of (fd != 0) should always succeed, unless your implementation defines stdin, stdout, stderr to something other than 0, 1, and 2.
You should be testing that they aren't -1 or, in general, < 0.
Thanks to both of you, for correcting my mistakes.
The problem now i got is "Error while writing to file".
I guess there is a problem in "dest_file = open(dest, O_CREAT, S_IRWXU)".
Is that ok to write into the file using O_CREAT?
I tried "close(dest_file);" and open it again by "dest_file = open(dest, O_WRONLY);".
Then i can write into it without writing error, but this way looks clumsy.
I read "man open" but i don't quite understand about O_CREAT.
If i create a file using "open(dest, O_CREAT, S_IRWXU)" but the file already exists, how can i overwrite it?
If i delete the existing file and create a new one with the same name, it looks clumsy again.
If i create a file using "open(dest, O_CREAT, S_IRWXU)" but the file already exists, how can i overwrite it?
Quote:
O_TRUNC
If the file already exists and is a regular file and the open mode allows writing (i.e., is O_RDWR or O_WRONLY) it will be truncated to length 0.
Quote:
"Error while writing to file"
That looks like it's from your code. Try altering your error message to print the system error message, with something like: printf("Error while writing to file: %s\n", strerror(errno));
For you copying.. this function might be intresting..
Parts taken from its manpage..
--
#include <sys/sendfile.h>
--
ssize_t sendfile(int out_fd, int in_fd, off_t *offset, size_t count);
--
Because this copying is done within the kernel, sendfile() does not
need to spend time transferring data to and from user space.
--
Originally posted by worldmagic For you copying.. this function might be intresting..
Parts taken from its manpage..
--
#include <sys/sendfile.h>
--
ssize_t sendfile(int out_fd, int in_fd, off_t *offset, size_t count);
--
Because this copying is done within the kernel, sendfile() does not
need to spend time transferring data to and from user space.
--
For my project it is not allowed to use function like this.
But thanks for telling, it may help in the future.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.