simOX is the executable file The c program that I need to wr

simOX is the executable file. The c++ program that I need to write needs me to open \"config_1.cnf\" which is the file name that I need to enter in command line. My quesiton is how I can open the \"config_1.cnf\" without asking the user the filename when the program runs.

2.4 the simulator Running When running the simulator you will be required to input a single configuration file (extension .cnf). You will run the simulator from the command line similar to the following: ./simox config-1.cnf The name of the assignment must be the simulator number. Many configuration files should be used to test your program, which you may modify for testing purposes as you see fit

Solution

You can do this by reading commandline argument in main function :

#include <fstream>
#include <iostream>

using namespace std;

int main ( int argc, char *argv[] )
{
if ( argc != 2 )
// argv[0] assuming it is the program name
cout<<\"enter: \"<< argv[0] <<\" <filename>\ \";
else {
// Assume argv[1] is a filename to open
ifstream inputfile ( argv[1] );

if ( !inputfile.is_open() )
cout<<\"Could not open file\ \";
}
}
}

simOX is the executable file. The c++ program that I need to write needs me to open \

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site