LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-07-2009, 06:05 AM   #1
Linux_Kid_
LQ Newbie
 
Registered: May 2009
Posts: 20

Rep: Reputation: 0
copying a file in c linux


Hi All,

I searched on google but could not find out any Linux API for copying file .Rename is for moving a file from a location to other,Is there any API for copy too.

or i have to copy byte by byte to a new file.

System(cp / /tmp) call is another alternative.Bt as i studied its not a good practice to use system call in C program..

Please help..

Thanks and Regards
 
Old 09-07-2009, 10:22 AM   #2
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Ubuntu 12.04, Antix19.3
Posts: 3,794

Rep: Reputation: 282Reputation: 282Reputation: 282
You can determine the filesize, allocate a buffer for that size and read the file into the buffer. Next write the buffer to a new file.

As far as I know, there is indeed no copy function.
 
Old 09-07-2009, 10:51 AM   #3
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
Copy in 1M chunks using a statically allocated buffer. This prevents nasty swapping or running out of memory for large files.
 
Old 09-07-2009, 11:02 AM   #4
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
I sometimes copy a file using mmap to avoid allocating a buffer. I don't know if this is actually faster, though.
Kevin Barry
 
Old 09-07-2009, 05:32 PM   #5
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
I always think the most efficient way is to use the read(2) and write(2) syscalls and call stat(2) on the destination directory first to find the optimal buffer size.

From "man 2 stat":
Quote:
The st_blksize field gives the "preferred" blocksize for efficient file system I/O. (Writing to a file in smaller chunks may cause an inefficient read-modify-rewrite.)
But when copying many small files to the same directory, it may become sub-optimal to call stat() for every file...

Last edited by Hko; 09-07-2009 at 05:34 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Issues copying script file from windows to linux and running hackop Linux - Software 2 04-25-2009 08:14 PM
must Linux really change file-date when copying? Sasie Linux - Newbie 6 03-12-2007 09:36 PM
copying a file from windows nt to slax linux distro mrobertson Linux - General 2 05-23-2005 10:20 PM
copying file in a network (linux server and window workstation) shadowsurfer Linux - Newbie 4 09-17-2004 11:15 AM
copying linux file to windows workstation shadowsurfer Linux - Networking 3 09-17-2004 09:54 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:47 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration