Just for the record, here's the C++ STL implementation for a little more tidiness
#include "DarkGDK.h"
#include <fstream> // std::ofstream and std::ifstream
#include <string.h> // strcpy
void DarkGDK ( void )
{
dbSyncOn ();
dbSyncRate (60);
// Save
std::ofstream outfile("MySuperText.txt"); // will overwrite any existing file
int aaa = -111;
outfile << aaa << std::endl; // writes -111 and a new line to text file
outfile << "good" << std::endl; // writes "good" and a new line to text file
outfile << "apple" << std::endl; // writes "apple" and a new line to text file
outfile.close();
// Load
std::ifstream infile("MySuperText.txt");
int bbb;
std::string string1, string2;
infile >> bbb; // reads -111 and converts it to int
infile >> string1; // reads "good" and converts to std::string
infile >> string2; // reads "apple" and converts to std::string
infile.close();
char buffer[16];
while (LoopGDK())
{
sprintf (buffer,"%d",bbb);
dbText (10,10,buffer); //Print number -111
dbText (10,20,const_cast<char*>(string1.c_str())); //Print word "good"
dbText (10,20,const_cast<char*>(string2.c_str())); //Print word "apple"
dbSync ( );
}
return;
}
std::string is a memory safe dynamically allocated string. You can use it just like you used it in DBP.
You can get a const char* from an std::string with:
std::string string1 = "hello world!"
const char* cMyString = myString.c_str();
But because DarkGDK failed to be const correct, dbText() accepts only a char*, so you are forced to cast away the qualifiers:
dbText(10, 10, const_cast<char*>(string1.c_str()));
That gets a little ugly I suppose, but it's not your fault. Alternately, you can copy the string into a buffer before printing it, just like what you'd do with integers:
char buffer[256]; // make absolutely sure the string is not longer than the buffer size
sprintf(buffer, "%s", string1.c_str());
dbText(10, 10, buffer);
I like offending people. People who get offended should be offended. --
Linux Torvalds