Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing file name through exe

Status
Not open for further replies.

NetworkGhost

IS-IT--Management
Apr 12, 2005
1,324
0
0
US
For this line I would like to be able to pass the filename to be opened rather than defining it in the code. Does anybody know if I can do this? And How?

basically it would look like so

mycode.exe /f configsource.txt


configData.open("configsource.txt", ios::in);
 
Code:
int main( int argc, char* argv[] )
{
   if ( argc < 2 )
   {
      cout << "You must specify a filename!" << endl;
      return 1;
   }

   string filename( argv[1] );
...
}
 
Note that cpjust's example doesn't actually do what you specified, but you can use it to figure out the right code for the job.

Also remember that argc is always at least 1, and argv[0] is the name or path to your executable, so any other arguments come after that.
 
That looks good the only issue is it seems I cant refer to a variable when using

configData.open(filename, ios::in);


Is there a trick to getting this to work?
 
The fstream constructors and open function take a C style string so you have to use:
Code:
configData.open(filename.c_str(), ios::in);
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top