Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is NOT a problem , just a curiosity question.
The attached C++ code creates "temp_file.txt " file.

I am curious if there is a hack to use variable instead of "literal (?) " for the file name.
My guess woudl be to modify the "hciconfig" redirecting command, but I am not that versed in doing so.

system("hciconfig -a > temp_file.txt ");


What I have tried:

Replacing the file name with variable ( char *) as file name did not give expected results.
Posted
Updated 17-Feb-20 3:57am

The argument of system is a string, you may generate such a string according to your needs.
For instance the following program
C++
#include <iostream>
#include <sstream>
#include <vector>
using namespace std;

int main()
{
  vector <const char *> names { "foo.txt", "boo.txt", "gooooo.txt" };

  for (const auto & name : names )
  {
    ostringstream oss;
    oss <<   "hciconfig -a > " << name;
    cout << oss.str() << endl; // here you are the customized command string
  }
}
produces
hciconfig -a > foo.txt
hciconfig -a > boo.txt
hciconfig -a > gooooo.txt
 
Share this answer
 
v2
It's just basic C string manipulation ...
C++
char* command = "hciconfig -a > ";
char* tempfile = "temp_file.txt";
char szFullCommand[64];
strcpy(szFullCommand, command);
strcat(szFullCommand, tempfile);
system(szFullCommand);
 
Share this answer
 
Comments
Vaclav_ 17-Feb-20 11:08am    
Both nice solutions, but...
it's the terminology which is puzzling
"char *" is a string in C
and
"string" x is a string in C++
Thanks
Richard MacCutchan 17-Feb-20 11:52am    
C++ enhanced things by creating basic classes to make it more object oriented. But it still supports the C types. If you are writing C++ code then you should really use only C++ classes for any compound objects (e.g. string).

Strictly speaking char* is a pointer to an array of char types, not a string, as C does not have any concept of compound types.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900