[wellylug] Programming Question

Jonathan Harker jharker at massey.ac.nz
Mon Dec 1 10:38:15 NZDT 2003


Jethro Carr wrote:

> On Thu, 2003-11-27 at 20:55, Chris Harris wrote:
> 
>>>>> hi,
> 
> 
>>/* docp.c */
>>int main(){
>>  system("cp docp.c docp.copy&");
>>  return 0;
>>}
> 
> 
> Ahh, a new question....
> 
> How can I use a variable for the input, ie:
> 
> system("touch <variable>");
> 
> thanks,

Hi there,

Some advice... if you are using C/C++, then you may as well use C++. They are 
not the same thing, and you can learn C++ without having to learn C. This is 
handy because you can avoid learning how to fluff about reallocating memory 
for char* buffers, all the tricks to get around the abysmally insecure 
printf/scanf family of functions, and so on.

Instead, you can use the C++ standard library to do string manipulation just 
as nicely as any other language, and use std::string wherever you would 
normally use a C style char* string. They handle memory allocation 
automatically for you.

#include <string>
#include <cstdlib>
using namespace std;

int main()
{
   string myfilename = "path/to/somefile";
   string mycommand = "touch " + myfilename;
   system(mycommand);
}

A good introduction to C++ is the Bruce Eckel book "Thinking in C++". It is 
available in good bookshops or online at www.bruceeckel.com, and I have a 
mirror of it at my Massey site:

http://www.massey.ac.nz/~jdharker/docs/

Cheers,
Jonathan.





More information about the wellylug mailing list