Computing.Net > Forums > Unix > unix command in c

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

unix command in c

Reply to Message Icon

Name: Sun
Date: June 13, 2006 at 02:42:56 Pacific
OS: Win-xp, Unix
CPU/Ram: --
Product: --
Comment:

Hi,
I want to copy lines from a file, by using cp command in my C program, to a directory

Thankx in advanced



Sponsored Link
Ads by Google

Response Number 1
Name: nails
Date: June 13, 2006 at 18:34:44 Pacific
Reply:

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



0

Response Number 2
Name: Sun
Date: June 15, 2006 at 23:16:12 Pacific
Reply:

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


0

Response Number 3
Name: nails
Date: June 16, 2006 at 12:38:38 Pacific
Reply:


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.


0

Response Number 4
Name: Sun
Date: June 17, 2006 at 00:41:53 Pacific
Reply:

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


0

Response Number 5
Name: nails
Date: June 17, 2006 at 08:51:47 Pacific
Reply:

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.


0

Related Posts

See More



Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

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


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: unix command in c

Run Unix command in DOS FTP prompt. www.computing.net/answers/unix/run-unix-command-in-dos-ftp-prompt/8494.html

UNIX commands in a networked enviroment www.computing.net/answers/unix/unix-commands-in-a-networked-enviroment/1981.html

Running Unix commands in parallel www.computing.net/answers/unix/running-unix-commands-in-parallel/6259.html