So, I'm coding for a C++ program (well, actually a .dll plugin for RpgMaker 2003), and I have a code like this:
#include <DynRPG/DynRPG.h>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
std::map<std::string, std::string> configuration;
bool onStartup(char *pluginName) {
// We load the configuration from the DynRPG.ini file here
configuration = RPG::loadConfiguration(pluginName);
return true; // Don't forget to return true so that the start of the game will continue!
}
bool onComment( const char* text,
const RPG::ParsedCommentData* parsedData,
RPG::EventScriptLine* nextScriptLine,
RPG::EventScriptData* scriptData,
int eventId,
int pageId,
int lineId,
int* nextLineId )
{
std::string cmd = parsedData->command;
std::string filecode;
std::string scanfile;
string FileName;
if(!cmd.compare("file_string")) //new code
{
ofstream myfile;
myfile.open ("dynstore.txt", ios::app);
myfile << parsedData->parameters[0].text << "\n";
myfile.close();
return false;
}
if(!cmd.compare("store_file")) //new code
{
ofstream myfile;
myfile.open ("dynstore.txt", ios::app);
myfile << RPG::actors[parsedData->parameters[0].number]->name << "\n";
myfile.close();
return false;
}
if(!cmd.compare("load_file")) //new code
{
ifstream myfile;
myfile.open ("dynstore.txt");
if(myfile.good()) //this should skip if the file doesn't exist.
{
getline (myfile, filecode);
RPG::actors[parsedData->parameters[0].number]->name = filecode;
}
myfile.close();
return false;
}
//These first three allow only storage using dynstore.
if(!cmd.compare("create_file")) //custom file name, plus storing text
{
FileName = parsedData->parameters[0].text;
ofstream myfile;
myfile.open (FileName.c_str(), ios::app);
myfile << parsedData->parameters[1].text << "\n";
myfile.close();
return false;
}
//This stores a word and makes a new line
if(!cmd.compare("create_save")) //custom file name, plus storing text
{
int GameFile = RPG::variables[3351];
ofstream myfile;
if(GameFile == 0)
{
FileName = parsedData->parameters[0].text;
myfile.open (FileName.c_str(), ios::app);
}
if(GameFile == 1)
{
myfile.open ("Save01.txt", ios::app);
}
if(GameFile == 2)
{
myfile.open ("Save02.txt", ios::app);
}
if(GameFile == 3)
{
myfile.open ("Save03.txt", ios::app);
}
if(GameFile == 4)
{
myfile.open ("Save04.txt", ios::app);
}
if(GameFile == 5)
{
myfile.open ("Save05.txt", ios::app);
}
if(GameFile == 6)
{
myfile.open ("Save06.txt", ios::app);
}
myfile << parsedData->parameters[1].text << "\n";
myfile.close();
return false;
}
//This stores a word and makes a new line
if(!cmd.compare("make_file")) //custom file name, plus storing text
{
FileName = parsedData->parameters[0].text;
ofstream myfile;
myfile.open (FileName.c_str(), ios::app);
myfile << RPG::actors[parsedData->parameters[1].number]->name;
myfile.close();
return false;
}
//This stores a name
if(!cmd.compare("read_file")) //sets data info to name for easy read
{
FileName = parsedData->parameters[0].text;
ifstream myfile;
myfile.open (FileName.c_str());
if(myfile.good()) //this should skip if the file doesn't exist.
{
getline (myfile, filecode);
RPG::actors[parsedData->parameters[1].number]->name = filecode;
}
myfile.close();
return false;
}
if(!cmd.compare("clear_file")) //sets data info to name for easy read
{
FileName = parsedData->parameters[0].text;
ofstream myfile;
myfile.open (FileName.c_str());
myfile.clear();
myfile.close();
return false;
}
if(!cmd.compare("scan_save"))
{
//Attempt ONE to scan the file for a string
//this one works by turning on a switch if the string is found
int GameFile = RPG::variables[3351];
ifstream myfile;
if(GameFile == 0)
{
FileName = parsedData->parameters[0].text;
myfile.open (FileName.c_str());
}
if(GameFile == 1)
{
myfile.open ("Save01.txt");
}
if(GameFile == 2)
{
myfile.open ("Save02.txt");
}
if(GameFile == 3)
{
myfile.open ("Save03.txt");
}
if(GameFile == 4)
{
myfile.open ("Save04.txt");
}
if(GameFile == 5)
{
myfile.open ("Save05.txt");
}
if(GameFile == 6)
{
myfile.open ("Save06.txt");
}
scanfile = parsedData->parameters[1].text;
int switchnum = parsedData->parameters[2].number;
if(myfile.good()) //this should skip if the file doesn't exist.
{
for(std::string temp;!myfile.eof();std::getline(myfile,temp))
{
if(temp.find(scanfile)!=std::string::npos)
{
RPG::switches[switchnum] = true;
//Turns on the switch if the string has been found in the file
//ideally, I'd want to save a new name using the scanfile, but I can't figure out how to copy it
}
}
}
myfile.close();
return false;
} //This checks only the current save, defaulting to the input if none exists
if(!cmd.compare("scan_file"))
{
//Attempt ONE to scan the file for a string
//this one works by turning on a switch if the string is found
FileName = parsedData->parameters[0].text;
ifstream myfile;
myfile.open (FileName.c_str());
scanfile = parsedData->parameters[1].text;
int switchnum = parsedData->parameters[2].number;
if(myfile.good()) //this should skip if the file doesn't exist.
{
for(std::string temp;!myfile.eof();std::getline(myfile,temp))
{
if(temp.find(scanfile)!=std::string::npos)
{
RPG::switches[switchnum] = true;
//Turns on the switch if the string has been found in the file
//ideally, I'd want to save a new name using the scanfile, but I can't figure out how to copy it
}
}
}
myfile.close();
return false;
}
if(!cmd.compare("file_good"))
{
//Checks for the existence of a file.
FileName = parsedData->parameters[0].text;
ifstream myfile;
myfile.open (FileName.c_str());
int switchnum = parsedData->parameters[1].number;
if(myfile.good()) //this should skip if the file doesn't exist.
{
RPG::switches[switchnum] = true;
}
else
{
RPG::switches[switchnum] = false;
}
myfile.close();
return false;
}
//End of cmd
return true;
}
Generally, this is how the .dll plugin works: It runs the DynRPG headers through the linker settings, and the RPG_RT.exe of RpgMaker as its host arguments. onStartup and onComment are DynRPG-specific codes to run compatibly with RpgMaker. You don't need to know most of this, except to know that these if(!cmd.compare(" ")) codes basically are commands for RpgMaker. So, I am telling various files to open text files, but I've been having problems:
- I have a code to clear file, and it does clear the file. But I think it creates the file in order to clear it, rather than skipping over if it doesn't exist. In fact, I think this is a persistent issue, I dunno how to get the code to skip over nonexistent file names, so it makes new files if the code checks files.
- If possible, that clear file should actually delete the files rather than just making it blank.
- It appends information into the file I make (and there doesn't seem to be any memory leak), but it doesn't check to make sure that I haven't already entered the same word or phrase. When I'm writing into the file, I want to check But I think I only know how to check output when loading output, not before input. I want to avoid duplicates. Currently, I have a file called "awards.txt" (unlockable awards) and I have the file with like 6 instances of the same word. I want it to write the same code only once. This is my main problem.
Don't give obtuse advice, assuming I will understand what you mean. Show me actual code. I barely pieced together this much code from reading and adapting other tutorials, and I do not have a degree in computer science (I was a history major, so the theory of C++ code doesn't always make sense to me). Oh and uhhhh, not all the header files are necessary, I tend to include more than I need just in case.