Hi, I've been trying to tell my new program to check for a specific condition (whether it is the first execution or not), but I'm having quite a bit of trouble. In fact, it's driving me insane. I spent almost all of last night trying to get this right, but it simply will not work the way I want it to. I've asked a huge number of people for help on message boards across the net, and nobody has been willing to help me fix it. So this is my last chance. PLEASE help me. Anyways, here's what I have so far, see if you can fix it and point out what I'm doing wrong:
Prototype:
int Config (int firstrun);
Cut from main():
int firstrun;
Config(firstrun);
int temp1 = 1;
int temp2 = 0;
if (firstrun == temp1)
{
cout << "Generating autoexec.cfg...\n";
ofstream config;
config.open ("firstruncheck.sece", ios::out | ios::trunc);
config << "0";
config.close();
ofstream autoexec;
autoexec.open ("autoexec.cfg", ios::out | ios::app);
autoexec << "\n// SECE execution commands...\n";
autoexec << "exec burter_cvars.cfg\n";
autoexec << "exec android17_cvars.cfg\n";
autoexec << "exec guldo_cvars.cfg\n";
autoexec << "exec jeice_cvars.cfg\n";
autoexec << "exec recoome_cvars.cfg\n";
autoexec << "exec android18_cvars.cfg\n";
autoexec << "exec cell_cvars.cfg\n";
autoexec << "exec buu_cvars.cfg\n";
autoexec << "exec frieza_cvars.cfg\n";
autoexec << "exec ginyu_cvars.cfg\n";
autoexec << "exec gohan_cvars.cfg\n";
autoexec << "exec goku_cvars.cfg\n";
autoexec << "exec krillin_cvars.cfg\n";
autoexec << "exec piccolo_cvars.cfg\n";
autoexec << "exec vegeta_cvars.cfg\n";
autoexec << "exec trunks_cvars.cfg";
autoexec.close();
}
else if (firstrun == temp2)
{
cout << "The program has executed normally...\n";
}
else
{
cout << "Error: unable to read value from firstruncheck.sece!\n";
}
Function:
int Config (int firstrun)
{
int temp1 = 1;
int temp2 = 0;
ifstream config;
config.open ("firstruncheck.sece", ios::in);
if (config.is_open())
{
config >> firstrun;
if (firstrun == temp1)
{
cout << "Config() executed normally...\n";
cout << "Firstrun: yes" << endl;
return(firstrun);
}
else if (firstrun == temp2)
{
cout << "Config() executed normally...\n";
cout << "Firstrun: no" << endl;
return(firstrun);
}
else
{
cout << "Error: unable to read value from firstruncheck.sece!\n";
return(firstrun);
}
}
else
{
config.close();
cout << "Error: unable to read value from firstruncheck.sece!\n";
main();
}
}

First Execution Only...
NCGordon
I order to minimize the number of adjustments, I think you can try changing of the Config function only. Instead of
try this:
I hope this helps. (You can also consider a Config function having no parameters and assigning of returned value to firstnum variable).
steuerlt
Shruti00
I think I understand. I think you want the program to do something only once, even if it has done it days or even years before. Is there any preference or requirements for how to do that Perhaps you can write something in a file that indicates whether the intialization has been performed. Or perhaps you can create a registry entry.
In fact, I think that what you want to do is to create the file "autoexec.cfg" only once, correct So why not simply check to see if the file exists If it exists, assume it has been created by your program. If it does not exist, create it. Would that work
Danny Tuppeny
I think part of the answer I posted a couple of minutes ago for another question is the answer here too. I will first explain a few other things.
When posting sample code, it helps to make the code as simple as possible, and not just because I like Simple Samples. The smaller the sample that you provide is, the easier it is for others to understand the problem. Many times, people (at least I) solve their problem themselves when they create a separate small project to duplicate the problem and work with it. Your sample is small but it could be simplified further. Certainly it is easier for you to post what you have, but it can be easier in the end if you take the time to create a sample that is as minimal as possible.
The problem I think you have encountered is explained in all good books and tutorials about C/C++. It is more efficient for you to get explanations from pre-existing resources such as that. So if the following explanation does apply, then be assured that you can get many more answers such as this from books and tutorials.
When a function is called, the parameters are put in a temporary location; usually that temporary location is the stack. The contents of the stack (the portion containing the function's parameters) are discarded when the function returns. So in your case, when the main function calls Config(firstrun), the contents of firstrun are put in the stack; but it is a copy of the contents. So when Config modifies firstrun, it modifies a copy of it, not the value that the caller (main) has. So when "if (firstrun == temp1)" is executed, firstrun still has the value it had before the call to Config.
Does that explain the problem The solution is to pass a pointer to firstrun to Config. Do you know how to do that
imj
if (firstrun >= temp1)
{
cout << "Generating autoexec.cfg...\n";
ofstream config;
config.open ("firstruncheck.sece", ios::out | ios::trunc);
config << "no";
config.close();
ofstream autoexec;
autoexec.open ("autoexec.cfg", ios::out | ios::app);
autoexec << "\n// SECE execution commands...\n";
autoexec << "exec burter_cvars.cfg\n";
autoexec << "exec android17_cvars.cfg\n";
autoexec << "exec guldo_cvars.cfg\n";
autoexec << "exec jeice_cvars.cfg\n";
autoexec << "exec recoome_cvars.cfg\n";
autoexec << "exec android18_cvars.cfg\n";
autoexec << "exec cell_cvars.cfg\n";
autoexec << "exec buu_cvars.cfg\n";
autoexec << "exec frieza_cvars.cfg\n";
autoexec << "exec ginyu_cvars.cfg\n";
autoexec << "exec gohan_cvars.cfg\n";
autoexec << "exec goku_cvars.cfg\n";
autoexec << "exec krillin_cvars.cfg\n";
autoexec << "exec piccolo_cvars.cfg\n";
autoexec << "exec vegeta_cvars.cfg\n";
autoexec << "exec trunks_cvars.cfg";
autoexec.close();
}
else if (firstrun < temp1)
{
cout << "The program has executed normally...\n";
}
else
{
cout << "\a";
cout << "Error: unable to read value from firstruncheck.sece!\n";
}
What about in main() Do I change anything
Jamie Thomson
vhmau
Bill Calkins
Change to:
The "p" in "pfirstrun" is not important; it is a convention to help us remeber it is a pointer.
Change to:
Change to:
As above. Within Config(), use "*pfirstrun" instead of "firstrun". Please find a book or tutorial for an explanation.
doubletree
Hmm, that explains alot. Actually, that explains A LOT of problems I've been having, not just with this program, but with many in the past. Thank you so very much.
And to answer your question, no, I don't know how to do that. :(
billg51
Raguvind
You've got the idea, yeah. Only, what I'm trying to do is print a number of lines to autoexec.cfg IF firstruncheck.sece (the file that holds the value for whether it is the program's first execution or not) indicates that it's the program's first execution. If it's the first execution, it should change the value in the file to indicate that it has already been executed, and thus it is not the first execution for all of the following executions. Still with me Good. Now for the problem. Looking at my code, you would probably say, "yeah, that looks correct", but somehow it isn't. It compiles without any errors, but the program always sees an execution as being the first execution, no matter how many times I've tried to execute it prior. That's what I've been trying to fix for the past 24 hours, but I just can't get it right. That's why I'm here, on this board, right now, asking for help...
Arun Manglick
Yuemingzhang
By detecting whether the application is already started or not...
JustinA1
No, that isn't what I'm trying to do... *sigh*
Okay, let's try to explain this again:
I want a set of code to be executed, but only the very first time the program is ever run. The second time you run the program, it should branch off to a different set of code. See now