Hello everyone,
I am quite new in C++ and need to learn some new features and I need your help for a problem. I have a small mathemtical model which is solved by gurobi interface. I need to test this model for several instances which are kept in .dat file. How can I read and test all these files (there are five files to be tested) concesutively.
My code and model works well only for one data file as follows:
int main(int argc, char **argv)
{
constchar* filename = "C:..../Debug/Ins_1.dat";
if (argc > 1)
filename = argv[1];
ifstream file(filename);
if (!file) {
cerr << "ERROR: could not open file '" << filename << "' for reading" << endl;
cerr << "ERROR: could not open file for reading" << endl;
cerr << "usage: " << argv[0] << " <file>" << endl;
throw(-1);
}
………………………………
………………….
……………………// there is model here I coded,
gurobi.solve()
return 0;
}
How can I read/open and solve the Ins_2, Ins_3, Ins_4, Ins_5 consecutively, automatically?
void doOneFile(constchar *fileName) {
ifstream file(filename);
if (!file) {
cerr << "ERROR: could not open file '" << filename << "' for reading" << endl;
cerr << "ERROR: could not open file for reading" << endl;
cerr << "usage: " << argv[0] << " <file>" << endl;
throw(-1);
}
………………………………
………………….
……………………// there is model here I coded,
gurobi.solve()
}
int main(int argc, constchar *argv) {
if (argc ==1 ) {
doOneFile("C:..../Debug/Ins_1.dat");
} else {
for (int i=1; i<argc; ++i) {
doOneFile(argv[i]);
}
}
}
Then run the program from the command line:
myProg file1.dat file2.dat ...
In Linux you can do:
myProg *.dat
I'm not sure if windows expands the list these days. It's been years since I programmed anything for a Microsloth OS.
Thank you so much for your anwser @dhayden , I really need to solve this part. Because, solving the models one by one manually will be so energy consuming for me. If we can solve it, I will be very happy. I tried your way but I get an error with
doOneFile(argv[i]); in the main block , it says argv is not compatible with the const char type.
I guess with the same resaon, ifstream file(filename); in the void fucntion is underline with red, since it does not recognize filename . How can I solve these parts ? I am Windows user btw
Thank you so much
I guess with the same reason, ifstream file(filename); in the void fucntion is underline with red
No it is underlined in red because you did not notice the difference in the spelling. Case does make a difference.
In the function definition void doOneFile(constchar *fileName).
Inside the function ifstream file(filename);. Choose 1 or the other.
In order to use "argv[0]" in the function first you need to pass it to the function.
As far as I know for Windows you either define "argv" as, you did, char **argv or char *argv[]. Putting "const" in front of either means that the program can not change these values.
However you define "argv" in "main" you will need to define it the same way in any function that uses "argv".
void doOneFile(char *filename) {
ifstream file(filename);
if (!file) {
cerr << "ERROR: could not open file '" << filename << "' for reading" << endl;
cerr << "ERROR: could not open file for reading" << endl;
cerr << "usage: " << argv[0] << " <file>" << endl;
throw(-1);
}
………………………………
………………….
……………………// there is model here I coded,
gurobi.solve()
}
int main(int argc, char *argv[]) {
if (argc ==1 ) {
doOneFile("C:..../Debug/Ins_1.dat");
} else {
for (int i=1; i<argc; ++i) {
doOneFile(argv[i]);
}
}
}
I do not get any error for the main function but , there , I can not understand how the Ins_1.dat file will be iterated until 5 (Ins_5.dat). We do not define 5 anywhere. I know I am asking many questions , sorry , but I really want to understand the logic. Is there any other way to run the instances except command window?
Thank you so much
Sorry for all the confusion. As I was typing my answer, I thought "I really should try to compile this to make sure I don't have any dumb syntax errors.... nah....
Thank you so much to everyone. I can not iterate the files, can you tell me what should I write in command window to test the files Ins_1.dat, Ins_2.dat, Ins_3.dat, Ins_4.dat and Ins_5.dat. I opened the command window and written the directory where the source.cpp is found, entered but Nothing happened. Is there any way to run the instances except command window ? or what should I write exactly in command window
The parameters need to be the path to the files. If they aren't in the current directory where you're running the command window, then you'll need to give the path.
Yes the path is correct , so when I run the code the first file which is called Ins_1 works and the results are shown but it does not go on to the rest; Ins_2, Ins_3 etc.(all the files are in the same directory btw) I do not run the code in command screen btw, because I do not know what to write exactly there. I just click on debugging on C++ . It would be very nice if we can solve this issue, thank you so much
Thank you so much @ seeplus , you solved my problem! I thank all the people in this discussion, I really learnt a lot.
One last thing, when I run the code of seeplus, I need to press any key after each Ins_.dat file has been treated. Can I do it automatically? I mean from file 1to 3 , all the files must be treated automatically, without pressing any key to continue between the transactions. How can I do that?