IT Community - Software Programming, Web Development and Technical Support

How can I call system in C?

This is a discussion on How can I call system in C? within the C and C++ Programming forums, part of the Software Development category; How can I call system when parameters (filenames, etc.) of the executed command aren't known until run time?...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Software Development > C and C++ Programming

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 07-17-2007, 03:40 AM
Sabari Sabari is offline
D-Web Genius
 
Join Date: Jul 2007
Posts: 1,008
Sabari is on a distinguished road
Default How can I call system in C?

How can I call system when parameters (filenames, etc.) of the executed command aren't known until run time?
__________________
Thanks & Regards
Sabari...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-17-2007, 03:57 AM
prasath prasath is offline
D-Web Sr.Programmer
 
Join Date: Jul 2007
Location: Chennai
Posts: 173
prasath is on a distinguished road
Default Re: How can I call system in C?

Hi,
when use sprintf (or perhaps strcpy and strcat) to build the command string in a buffer, then call system with that buffer. (Make sure the buffer is allocated with enough space

Here is a contrived example suggesting how you might build a data file, then sort it (assuming the existence of a sort utility, and Unix- or MS-DOS-style input/output redirection):

char *datafile = "file.dat";
char *sortedfile = "file.sort";
char cmdbuf[50];
FILE *fp = fopen(datafile, "w");

/* ...write to fp to build data file... */

fclose(fp);

sprintf(cmdbuf, "sort < %s > %s", datafile, sortedfile);
system(cmdbuf);

fp = fopen(sortedfile, "r");
/* ...now read sorted data from fp... */

Thanks
Prasath.K
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Difference between System.String and System.StringBuilder class? S.Vinothkumar C# Programming 1 10-29-2007 05:23 AM
difference between call by value and call by reference amansundar Java Programming 2 09-18-2007 01:34 AM
What’s the difference between the System.Array.CopyTo() and System.Array.Clone() ? Archer C# Programming 2 08-25-2007 03:00 AM
Function call and System call vigneshgets Operating Systems 1 08-01-2007 06:18 AM
How Do I Call Java from C? kingmaker C and C++ Programming 1 07-30-2007 07:58 AM


All times are GMT -7. The time now is 07:30 AM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0