LinuxQuestions.org
Review your favorite Linux distribution.
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 06-28-2001, 03:36 AM   #1
beta
LQ Newbie
 
Registered: Jun 2001
Posts: 13

Rep: Reputation: 0
Programming with files in C


Hello everyone...
I wan't to move a file with a C program to a different dircetory. How can I do that?
 
Old 06-28-2001, 05:34 AM   #2
isajera
Senior Member
 
Registered: Jun 2001
Posts: 1,635

Rep: Reputation: 45
hmmm... well, my immediate answer is to move the file to the directory.

if you could explain the problem a bit more specifically, we could help a lot more.
 
Old 06-28-2001, 07:03 AM   #3
beta
LQ Newbie
 
Registered: Jun 2001
Posts: 13

Original Poster
Rep: Reputation: 0
I wan't to write a program which sorts files for me.
for example it should move all files beginning with old_ to the directory /trash. It has to be a C program. (I know that that would be really easy with an shell-script)
 
Old 06-28-2001, 07:28 AM   #4
SensFan
Member
 
Registered: May 2001
Location: Toronto, Canada
Distribution: Slackware
Posts: 67

Rep: Reputation: 15
Maybe popen is what you need, it will envoke a pipe to the shell and allow you to execute shell commands, in this case cp.

#include <stdio.h>

FILE *popen(const char *command, const char *type);

int pclose(FILE *stream);
 
Old 06-28-2001, 11:25 PM   #5
crabboy
Senior Member
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,821

Rep: Reputation: 121Reputation: 121
The easiest way to move files in a C program is to call link. This will create a hard link to an existing file. Once your link is there you can remove the old link. Here is a simple sample program:

Code:
#include <unistd.h>

main( int argc, char *argv[])
{
   char strOldFile[256];
   char strNewFile[256];

   strncpy( strOldFile, argv[1], sizeof( strOldFile ) - 1 );
   sprintf( strNewFile, "trash/%s", argv[1] );

   link( strOldFile, strNewFile );
   unlink( strOldFile );
}
This program has about a thousand things that can fail. You must check the size of argv[1] before using sprintf. And the return code from link and unlink should be checked.

Using link may be easy, but it has it's set of problems. Hard links CANNOT cross file systems. For this reason any program that moves files should check for the "EXDEV" return code and do a copy instead. I wrote a simple copy program long ago:
http://www.linuxquestions.org/questi...?threadid=1402

You also mentioned that you need to search for string patterns in file names. Look at the function "fnmatch". This method checks a string for a regular expressions and returns true if thre is a match. So you could search for something like "^old_*" and it would match files like:
old_fart
old_dude

But Not:
crusty_slow_old_java


Hope this helps.

Gary
 
  


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
which programming language is used to do tcp/ip programming?? gajaykrishnan Linux - Networking 9 12-21-2012 05:16 AM
Difference between Top Down programming & Bottom up programming minil Programming 1 06-17-2005 02:42 AM
C programming help... about creating files borgy_t Programming 2 02-08-2005 04:35 AM
How program to read files with extension .dat y .cfg files COMTRADE in fedora 1? ivonne Linux - Software 0 11-22-2004 11:42 AM
How to filter files in files and files which are in a sub-directory with "grep"? Piero Linux - Newbie 9 08-29-2003 02:38 AM

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

All times are GMT -5. The time now is 10:46 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