Operating System - HP-UX
1827889 Members
1755 Online
109969 Solutions
New Discussion

Moving of files using c programming..

 
Henry Chua
Super Advisor

Moving of files using c programming..

Is it possible to move or copy files from one directories to other using C programming? I know u can use "link" for this.. but is it possible if u want to copy all the files in a folder to another location?.. Is it also possible to see wat is in the folder using c?
9 REPLIES 9
Biswajit Tripathy
Honored Contributor

Re: Moving of files using c programming..

Henry,
If you are planning to do some C programming on
HP-UX (or other Unix) environment, you should
really consider buying the following book:

Advanced Programming in the UNIX(R) Environment
by W. Richard Stevens
http://www.amazon.com/exec/obidos/ISBN%3D0201563177/104-5204610-5331128

The chap 3 and 4 deals with file and directory
manipulations and deals with the problem you are
trying to solve.

- Biswajit
:-)
Henry Chua
Super Advisor

Re: Moving of files using c programming..

Thanks Tripathy, for the reference..

but if I just want to move files from one path to the other can't I just used

link("/path/*", "/dest");

apparently it doesnt seems to work.
?
Biswajit Tripathy
Honored Contributor

Re: Moving of files using c programming..

Yes, it does not. Shell inperprets '*' as a wildcard,
but link() is a system call and may not associate
any special meaning to '*'. So you need to open the
directory file, find all the filenames in the directory
and run a for loop to copy each file to the dest dir
by using link() call. If you want the shell to copy the
files inside a C program, you need to use system()
call instead of link(). The way to do that would be
the following:

system ("cp /path/* /dest");

This will enable the C program to fork a shell and do
the copy. But this kind of defeates the whold
purpose of writing a C program instead of a shell
script.

- Biswajit
:-)
Elmar P. Kolkman
Honored Contributor

Re: Moving of files using c programming..

There is even more to it then that... The systemcall link only works on files on the same filesystem, like the ln command (for hardlinks, not symbolic links) also only works within one filesystem. If you want to move all the files, is it perhaps possible to just move the directory?!

Using system is the easy way out, otherwise need to view the documentation above or find a similar source of information, like the sources of the mv command (might check out linux sources, for instance) for inspiration.

Good Luck,

Elmar
Every problem has at least one solution. Only some solutions are harder to find.
Biswajit Tripathy
Honored Contributor

Re: Moving of files using c programming..

Elmar wrote:
> There is even more to it then that... The systemcall
> link only works on files on the same filesystem

Exactly. Using link() to copy from one filesystem to
another should return error and set errno to EXDEV.
I did not go into the details because Henry's exposure
to C is only couple of days old (see his other threads
on the topic).

- Biswajit
:-)
Stephen Keane
Honored Contributor

Re: Moving of files using c programming..

You should do a man on glob(3c) to see how to do the equivalent of /dir/*

This system call will give you an array of all files that match a search pattern. You can then step through the array for each file and do what you want with it. If you are interested I could post an example of its use.
Henry Chua
Super Advisor

Re: Moving of files using c programming..

Hi Guys,

Thank for the input, I have downloaded the source code for mv in the linux.. kinda hard to digest for my level, do anyone has codes I can use that suit my needs?..

Sorry for the trouble..

Henry
Biswajit Tripathy
Honored Contributor

Re: Moving of files using c programming..

You can always write a function copy() to do the
copy of the file. Something like:

void copy (char *srcFilename, char *dstFileName)
{
FILE *fsrc, *fdst;
char c;

fsrc=fopen (srcFileName, "r");
/* Add usual error check */
fdst=fopen (dstFileName, "w");
/*Add usual error check */
for ( ; !feof(fsrc); )
fputc (fgetc (fsrc), fdst);
fclose (fsrc);
fclose (fdst);
}

Note that I have not tested the above code and
it does not do a lot of error checking, so you need
to do that before use.

- Biswajit
:-)
Stephen Keane
Honored Contributor

Re: Moving of files using c programming..

This will give you a list of files in a folder (I've hard-coded the folder name, but you can easily make it a parameter).