Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hi,
I want to copy lines from a file, by using cp command in my C program, to a directory
Thankx in advanced

Your requirement is too general. You can call most any Unix command from a "C" program using the "C" system function.

Ok, I will go to the second part of my question -- it maybe clearer:
I want to use `find' command to search in specific path and find all files that start with 'ab' and end with one letter that differ. If I could apply that to all drives it would be better since the path that I want to search in is common for all drives except the change in drive letter.
Then, I want to delete that files
All that should be done in C program
Thankx in advance

First, there is no such "C" library that emulates the Unix tools - at least not one that I'm aware of. If you want to run unix tools in Windows, you need a Unix tool kit. A number are available. Probably the most famous in Cygwin available free for download at:http://www.cygwin.com
Another popular one is the MKS toolkit available for purchase at:
http://www.mks.com
Once one of shells is running, you can develop a "C" program that calls external unix commands like 'find' using the popen function call. In this example, I find all the files starting with 'cx' in the /home/nails/cdir, build a remove string, and use the system call to remove each file:
#include <stdio.h>
int main()
{
char exe_str[]="find /home/nails/cdir -type f -name 'cx*' -print";
char line[256], myrm[300];
FILE *ptr, *popen();if((ptr = popen(exe_str, "r")) == NULL)
perror("Couldn't open pipe");while (1)
{
if(fgets(line, 256, ptr) == NULL)
break;
strcpy(myrm, "rm ");
strcat(myrm, line);
system(myrm);
}
fclose(ptr);exit(0);
}
/* end program */The above was developed on Solaris Unix.
In closing, I'm sorry, but I still don't understand your find requirement.

Thankx for your response, they were helpful. However, I have this question:
Why don't you do like this:
#include <stdio>int main(){
system("find /home/nails/cdir -type f -name 'cx*' | xargs /bin/rm -f Rmove");
}
My knowledge in this field is modest and I want you help and advise me to find the best and robust way.
Thankx in advance

Your find command works fine, (although I don't know what 'Rmove' means) However,my "C" stub was just meant to illustratte the use of pipes (popen) in "C". If you need to operate on each file that returns from find you need to use pipes.

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |