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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

C++ saving input variable strings in FILE command 1

Status
Not open for further replies.

sunup2012

Programmer
Apr 7, 2001
1
US
I need to learn how to input a variable type "char string" in to some type of storage file that can remember the variable entered even when the computer shuts off or the program is restarted. the variable in question will not be reentered once entered. If variable "A" is entered as 'if'
then "A" would stay 'if' even when the program is restarted.
I hope this is enough information. Please include working complete C++ code samples if possible as that is the way I learn best.

thanks,
sunup2012
 
The easiest way to do this, as I beleive is to use the windows registry


#include <registry.hpp>
void __fastcall TForm1::FormCreate(TObject *Sender)
{
/*
the registry could be seen as a directory structure where Keys'
are the 'folders' and in there are the 'files'
To see what kind of information that can be stoored in the registry
please refer to BCB help files
*/
// here is a simple way to see if the value exists
AnsiString variable;
const String KeyStr = &quot;\\MyRegistry&quot;;
MyRegistry->RootKey = HKEY_LOCAL_MACHINE;
MyRegistry->OpenKey(KeyStr, false); // lets open the 'folder', do NOT create if it doesn't exist
if(MyRegistry->KeyExists(&quot;A&quot;)) // check if variable A exists in the registry
{
value = MyRegistry->ReadString(&quot;A&quot;); // read variable A
} // this line can be modified to write a string be replacing ReadString with WriteString
Edit1->Text = value;
MyRegistry->CloseKey();
}

For more information about TRegistry please refer to BCB help files Martin G Broman
mgb_svea@thevortex.com

DWS - Alpha Whitin Dead Wolf Society
Xaner Software, DB programer
 
This is one way to save variables to a file. You will need to include the following header after &quot;#pragma hdrstop&quot; in your program.
Code:
#include <fstream>

Now, let's assume that your variable is named MyVariable. You will need to open the file for output initially. You can do it like this:
Code:
// Open the file
ofstream VariableFile; // stream set up for writing
VariableFile.open(&quot;MyFile.INI&quot;);

// If the file can't be created, there won't be a handle to handle (pun intended)
if (!VariableFile)
{
    // Put some error warning here
    return; // get out of this code
}
else
{
    // output each variable
    VariableFile << MyVariable << endl; // the endl flushes the buffer and puts CR/NL
}

Now you will need to be able to read the file at some point. Use code similar to the following to read the same file:
Code:
// Open the file
ifstream VariableFile; // stream set up for reading
VariableFile.open(&quot;MyFile.INI&quot;);

// Process the file
if (!VariableFile)
{
    // Error opening file
    // Do something about warning the user, etc.
    return;
}
else
{
    // get my variable
    VaraibleFile >> MyVariable;
}

There are numerous ways to get data in and out of a file. I like to use the standard streams since I don't have to worry out what type of varaibles is being put out. The &quot;<<&quot; takes care of it for me. MyVariable could just as easily be a string, integer, or float and the code would still work. As a matter of fact, I often input all my data from files into strings and then convert them to what I believe they should be in try...catch[/i] statements. This way if there is an error on input, my program won't crash. (Hopefully! :) )
James P. Cottingham
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top