C Board  

Go Back   C Board > General Programming Boards > C Programming

Closed Thread
 
LinkBack Thread Tools Display Modes
Old 11-04-2009, 07:08 AM   #1
Registered User
 
Join Date: Oct 2009
Posts: 6
Smile Equivalent of Dos 'copy' in C ?

Hi all,

I've got another question. Is there a function in C for copy?
I saw this somewhere and wondering if there's one for 'copy':

remove("Filename.txt"); // equivalent to 'delete filename.txt in dos
rename("Fileold.txt", "Filenew.txt"); // equivalent to 'rename fileold.txt filenew.txt

what about .. copy file1.txt file2.txt .. is there one for C?

I tried copy, copyfile, filecopy all not working.

Thanks,
YetBo
YetBo is offline  
Old 11-04-2009, 07:13 AM   #2
Jack of many languages
 
Join Date: Nov 2007
Location: Katy, Texas
Posts: 1,931
No Copy function. You'll have to read and write the file. Or, you could use the system() function and pass the copy command (which will then read and write the file for you).
__________________
Mac and Windows cross platform programmer. Ruby lover.

Memorable Quotes From Recent Posts:

I can't remember.
Dino is offline  
Old 11-04-2009, 07:27 AM   #3
Registered User
 
Join Date: Oct 2009
Posts: 6
Hi Dino thanks for the quick reply.

Mind showing me how the system() function works.

And Happy Birthday !!!
YetBo is offline  
Old 11-04-2009, 07:38 AM   #4
Jack of many languages
 
Join Date: Nov 2007
Location: Katy, Texas
Posts: 1,931
Thanks!

system("copy a b") ;
__________________
Mac and Windows cross platform programmer. Ruby lover.

Memorable Quotes From Recent Posts:

I can't remember.
Dino is offline  
Old 11-04-2009, 04:33 PM   #5
Super Moderator
 
Join Date: Sep 2001
Posts: 4,680
The string passed to system is executed as though it was entered at the command line. This makes it nice & easy for some things - but whatever you put in there is OS-dependant, and not really part of C, per se. Sometimes it's viewed as bad style.

That being said, copy still works in Windows, on *nix you'd use the 'cp' command.
sean is offline  
Old 11-04-2009, 04:50 PM   #6
Senior software engineer
 
brewbuck's Avatar
 
Join Date: Mar 2007
Location: Portland, OR
Posts: 5,381
Actually, switching out to the system copy command with system() is probably the most painless method.

Copying a file is more than just creating a file with the same contents as some other file. You also need to preserve the creation date, file permissions, ownership, and any other attributes. Settings these attributes is a very non-portable operation, and in fact, it is impossible to implement a true file copy in C using only standard functions.

So if you're going to delve into non-portability (and you must, if you need to copy files), you may as well reduce the pain to the minimum level possible by just calling system().
__________________
"Congratulations on your purchase. To begin using your quantum computer, set the power switch to both off and on simultaneously." -- raftpeople@slashdot
brewbuck is offline  
Old 11-04-2009, 05:31 PM   #7
Registered User
 
Join Date: Nov 2008
Posts: 75
Quote:
Originally Posted by brewbuck View Post
Actually, switching out to the system copy command with system() is probably the most painless method.

Copying a file is more than just creating a file with the same contents as some other file. You also need to preserve the creation date, file permissions, ownership, and any other attributes. Settings these attributes is a very non-portable operation, and in fact, it is impossible to implement a true file copy in C using only standard functions.

So if you're going to delve into non-portability (and you must, if you need to copy files), you may as well reduce the pain to the minimum level possible by just calling system().
Well, I think it can be done in a portable way through glib.
MisterIO is offline  
Old 11-04-2009, 05:40 PM   #8
Senior software engineer
 
brewbuck's Avatar
 
Join Date: Mar 2007
Location: Portland, OR
Posts: 5,381
Quote:
Originally Posted by MisterIO View Post
Well, I think it can be done in a portable way through glib.
What exactly do you think glib does internally? It calls platform-specific functions. There's nothing wrong with that, but it's incorrect to say that linking to some library which hides the non-portable details somehow makes your code portable.

Your code still won't compile on any platform which isn't supported by glib. That's non-portable by definition.

Non-portability is not a bad thing. The portable core of C and C++ is extremely restricted in what it can do. Real programs HAVE to go outside of those bounds.
__________________
"Congratulations on your purchase. To begin using your quantum computer, set the power switch to both off and on simultaneously." -- raftpeople@slashdot
brewbuck is offline  
Old 11-04-2009, 05:42 PM   #9
Registered User
 
Join Date: Nov 2008
Posts: 75
Quote:
Originally Posted by brewbuck View Post
What exactly do you think glib does internally? It calls platform-specific functions. There's nothing wrong with that, but it's incorrect to say that linking to some library which hides the non-portable details somehow makes your code portable.

Your code still won't compile on any platform which isn't supported by glib. That's non-portable by definition.

Non-portability is not a bad thing. The portable core of C and C++ is extremely restricted in what it can do. Real programs HAVE to go outside of those bounds.
No, your definition of portability is wrong. By your definition, not even the c standard library would be considered portable.
MisterIO is offline  
Old 11-04-2009, 05:49 PM   #10
Super Moderator
 
Join Date: Sep 2001
Posts: 4,680
Quote:
No, your definition of portability is wrong.
What exactly is your definition of portability, then? Because what he said falls in line with every other statement I've heard about portability? The C standard library is a uniform interface that is standardized across all interfaces - but the code to implement is extremely platform dependant. Working with files, etc...

brewbuck - good point, I agree - I was sharing that more as an intro to the "system" function in general though. I think it's grossly overused by people who have recently discovered it.
sean is offline  
Old 11-04-2009, 05:53 PM   #11
Registered User
 
Join Date: Nov 2008
Posts: 75
Quote:
Originally Posted by sean View Post
What exactly is your definition of portability, then? Because what he said falls in line with every other statement I've heard about portability? The C standard library is a uniform interface that is standardized across all interfaces - but the code to implement is extremely platform dependant. Working with files, etc...

brewbuck - good point, I agree - I was sharing that more as an intro to the "system" function in general though. I think it's grossly overused by people who have recently discovered it.
I can simply use what he said about glib with the c standard library and prove that he's wrong:

What exactly do you think "the c standard library" does internally? It calls platform-specific functions.
MisterIO is offline  
Old 11-04-2009, 06:00 PM   #12
Senior software engineer
 
brewbuck's Avatar
 
Join Date: Mar 2007
Location: Portland, OR
Posts: 5,381
Quote:
Originally Posted by MisterIO View Post
I can simply use what he said about glib with the c standard library and prove that he's wrong:

What exactly do you think "the c standard library" does internally? It calls platform-specific functions.
The C library is a STANDARD which all compilers must provide in order to claim standards compliance. The features granted by the standard are required, and you can depend on them.

glib is not a standard, is not required to be present on a system, is not required to be operable on any possible system.
__________________
"Congratulations on your purchase. To begin using your quantum computer, set the power switch to both off and on simultaneously." -- raftpeople@slashdot
brewbuck is offline  
Old 11-04-2009, 06:06 PM   #13
Registered User
 
Join Date: Nov 2008
Posts: 75
Quote:
Originally Posted by brewbuck View Post
The C library is a STANDARD which all compilers must provide in order to claim standards compliance. The features granted by the standard are required, and you can depend on them.

glib is not a standard, is not required to be present on a system, is not required to be operable on any possible system.
That's completely unrelated to the concept of portability. Being standard and being portable are 2 completely different concept.
MisterIO is offline  
Old 11-04-2009, 09:42 PM   #14
Senior software engineer
 
brewbuck's Avatar
 
Join Date: Mar 2007
Location: Portland, OR
Posts: 5,381
Quote:
Originally Posted by MisterIO View Post
That's completely unrelated to the concept of portability. Being standard and being portable are 2 completely different concept.
If your code adheres to all standard C rules, then your code is portable to any environment with a standard C compiler. If you're claiming otherwise, I'd like to hear your argument.
__________________
"Congratulations on your purchase. To begin using your quantum computer, set the power switch to both off and on simultaneously." -- raftpeople@slashdot
brewbuck is offline  
Old 11-04-2009, 09:51 PM   #15
Registered User
 
Join Date: Nov 2008
Posts: 75
Quote:
Originally Posted by brewbuck View Post
If your code adheres to all standard C rules, then your code is portable to any environment with a standard C compiler. If you're claiming otherwise, I'd like to hear your argument.
Again, this is completely irrelevant to the previous discussion and I never said that. It just seems to me that you're unable to admit that you were wrong.
MisterIO is offline  
Closed Thread

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Creepy DOS and BIOS functions (need help!!!) James00 C Programming 9 05-05-2003 12:40 AM
how many old school DOS programmers are left? Waldo2k2 A Brief History of Cprogramming.com 23 02-01-2003 05:14 PM
File systems?? (Winxp -> DOS) Shadow Tech Board 4 01-06-2003 09:08 PM
Quick ? on copy constructor Traveller C++ Programming 3 05-03-2002 10:31 AM
Copy Constructor Help Jubba C++ Programming 2 11-07-2001 11:15 AM


All times are GMT -6. The time now is 03:46 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22