M_construct error when reading file contents - c++

I'm creating a game that requires a lot of setup. I decided to add a configuration file (.ini) and a reader to get the information. My ini file is setup as follows:
-cmd arg0 arg1
I initially only had one command which worked fine until I added a second. For whatever reason, I receive an std::logic_error when my command is not the first one.
// this works
-load w "someName"
// this doesn't
-delete "someName"
Here is the code used to read the file:
InitFileReader::InitFileReader()
{
std::string ini_file_path = "";
ini_file_path += PROGRAM_FOLDER;
ini_file_path += "\\blockgame.ini";
std::ifstream ini_file(ini_file_path);
if (!ini_file.is_open()) std::cout << "Couldn't find configuration file at " << ini_file_path << std::endl;
std::string line;
while(std::getline(ini_file, line))
{
if (starts_with(line, "-load "))
{
std::string arg0 = "";
unsigned int index = 6;
for (; index < line.size(); index++)
{
char c = line[index];
if (c == ' ') break;
arg0 += c;
}
std::string arg1 = "";
bool reached_quote = false;
for (; index < line.size(); index++)
{
char c = line[index];
if (c == ' ' && !reached_quote) continue;
if (c == '\"' && !reached_quote)
{
reached_quote = true;
continue;
}
else if (c == '\"') break;
arg1 += c;
}
sfr = new SaveFolderReader(arg1);
if (arg0 == "new")
{
sfr->new_header(DEFAULT_HEADER);
}
else if (arg0 == "def")
{
sfr->restore_header(DEFAULT_HEADER);
}
}
else if (starts_with(line, "-delete "))
{
std::string arg0 = "";
unsigned int index = 8;
for (; index < line.size(); index++)
{
char c = line[index];
if (c == ' ') break;
arg0 += c;
}
std::string world_path = "";
world_path += PROGRAM_FOLDER;
world_path += "\\save\\";
world_path += arg0;
if (rmdir(world_path.c_str()) != 0)
{
std::cout << "Error deleting world \"" << arg0 << "\"" << std::endl;
}
}
}
}
inline bool starts_with(const std::string& target, const std::string& prefix)
{
if (target.size() < prefix.size())
return false;
for (unsigned int i = 0; i < prefix.size(); i++)
{
if (target[i] != prefix[i])
return false;
}
return true;
}
The PROGRAM_FOLDER constant is just the parent folder of the path returned by argv[0] in main. I can't run the debugger on this code because the path changes to something strange when I do that.
I know that this error shows up because of a nullptr initialization of std::string but I still have no idea why this is happening.
I've tried typing in random characters into the config file and get the same result. It baffles me that the first if-statement works fine but the second one doesn't (when I use the -delete command).
Any suggestions would be of great use, thanks in advance!

It turns out that the error wasn't coming from that block of code at all. What was happening was that my game tried to load data in afterwards but because I didn't write the load command there was nothing to read from. I fixed my Debugger issue (For whatever reason the char[] I allocated stored different data when the debugger ran) and just reverted to std::strings.

Related

How to return a pointer to array of structs when reading XML using tinyXML2

I am trying to read the elements in an XML and store in a array of
struct and need to pass the pointer of this array to other functions.
However I have issue compiling in gnu, error message:
error: cannot
convert myRec to uint32_t {aka unsigned int}' in return
return *recs;
Tried to set myRec recs[count] without malloc, get an error of invalid pointer.
struct myRec
{
std::string one;
std::string two;
std::string three;
std::string four;
std::string five;
std::string six;
};
uint32_t count = 0;
XMLDocument doc;
doc.LoadFile(pFilename);
XMLElement* parent = doc.FirstChildElement("a");
XMLElement* child = parent->FirstChildElement("b");
XMLElement* e = child->FirstChildElement("c");
for (e = child->FirstChildElement("c"); e; e = e->NextSiblingElement("c"))
{
count++;
}
std::cout << "\n""Count = " << count << std::endl;
recs = (myRec *)malloc(6 *count * sizeof(myRec));
XMLElement *row = child->FirstChildElement();
if (count > 0)
{
--count;
count = (count < 0) ? 0 : count;
for (uint32_t i = 0; i <= count; i++)
{
while (row != NULL)
{
std::string six;
six = row->Attribute("ID");
recs[i].six = six;
XMLElement *col = row->FirstChildElement();
while (col != NULL)
{
std::string sKey;
std::string sVal;
char *sTemp1 = (char *)col->Value();
if (sTemp1 != NULL) {
sKey = static_cast<std::string>(sTemp1);
}
else {
sKey = "";
}
char *sTemp2 = (char *)col->GetText();
if (sTemp2 != NULL) {
sVal = static_cast<std::string>(sTemp2);
}
else {
sVal = "";
}
if (sKey == "one") {
recs[i].one = sVal;
}
if (sKey == "two") {
recs[i].two = sVal;
}
if (sKey == "three") {
recs[i].three = sVal;
}
if (sKey == "four") {
recs[i].four = sVal;
}
if (sKey == "five") {
recs[i].five = sVal;
}
col = col->NextSiblingElement();
}// end while col
std::cout << "\n""one = " << recs[i].one << "\n"" two= " << recs[i].two << "\n""three = " << recs[i].three << "\n""four = " << recs[i].four << "\n""five = " << recs[i].five << "\n""six = " << recs[i].six << std::endl;
row = row->NextSiblingElement();
}// end while row
}
}
else
{
std::cout << "Failed to find value, please check XML! \n" << std::endl;
}
return *recs;
expect to return a pointer to the array
I declared it as:
std::string getxmlcontent(const char* pFilename);
myRec*recs= NULL;
Function:
std::string readxml::getxmlcontent(const char* pFilename)
{
}
Not sure if it is the right way as I am quite new to c++
You're making a few errors, you probably should get a good C++ book and do some studying
In C++ use new
recs = new myRec[6*count];
instead of
recs = (myRec *)malloc(6 *count * sizeof(myRec));
The problem with malloc in a C++ program is that it won't call constructors, so all the strings you have in your struct are invalid, and (most likely) your program will crash when you run it.
It's not clear to me why you need 6*count, that seems to be because you have six strings in your struct. If so then that's confused thinking, really you just need
recs = new myRec[count];
and you'll get 6*count strings because that's how you declared your struct.
sKey = sTemp1;
instead of
sKey = static_cast<std::string>(sTemp1);
No need for the cast, it's perfectly legal to assign a char* to a std::string.
Finally if you want to return a pointer, then just return the pointer
return recs;
not
return *recs;
However you haven't included the function signature in the code you posted. I suspect there's another error, but I can't tell unless you post how you declare this function.

Adding Space to String at certain places in C++

I am having trouble figuring out the process to add a space in a string at capital letters in C++. If I have a string "HelloWorld", how do I convert that to "Hello World"?
I've tried using substrings and the isupper function but I can't get anything to work.
Edit: this is the code I have. I don't understand why in = newname is not valid code.
string breakStringAtCaps(string in) {
string newname[10];
int j = 0;
for (int i = 0; i < in.size(); i++) {
if (isupper(in[i]) && i != 0) {
newname[j] = " ";
j++;
}
newname[j] = in[i];
j++;
}
in = newname;
return in;
}
You can do it like this:
string breakStringAtCaps(const string& in)
{
string newname;
for(int i = 0; i < in.size(); i++)
{
if(isupper(in[i]) && i != 0)
newname += " ";
newname += in[i];
}
return newname;
}
You are thinking right in thinking substr, but you implementation is a bit off. If creating an new string containing the contents of the original and inserting a ' ' (space) before each upper-case letter (not including the first), you can seed the new string with the first character of the original using substr(0,1) and then use an auto ranged for loop and substr(1) to evaluate each character after the first.
The loop along with a check of isupper() is basically all you need, e.g.
#include <iostream>
#include <string>
#include <cctype>
int main (int argc, char **argv) {
std::string s = argc > 1 ? argv[1] : "HelloWorld",
snew = s.substr (0,1);
if (s.size() > 0)
for (auto& c : s.substr(1)) {
if (std::isupper (c))
snew += " ";
snew += c;
}
std::cout << snew << '\n';
}
(the program will use "HelloWorld" by default if no string is given as an argument on the command line, otherwise the string given on the command line is used)
Example Use/Output
$ ./bin/spacebeforeupper
Hello World
Or with a string given as an argument:
$ ./bin/spacebeforeupper MyDogHasFleas
My Dog Has Fleas
You can iterate through the strin characters, check if it is a cap and insert a ' ' before if it is one:
It should look like:
for(int i=0; i < str.length(); i++)
{
if (str[i]>='A' && str[i]<='Z')
{
if (i != 0)
cout << " ";
cout << str[i];
}
else
{
cout << str[i];
}
}
I just give a implementation, maybe not the best solution:
#include <string>
#include <ctype.h>
void breakStringAtCaps(std::string& in) {
std::string::const_iterator it = in.begin();
while(it != in.end()) {
if(it != in.begin() && isupper(*it)) {
in.insert(it, ' ');
it += 2;
}
else
++it;
}
}
//
#include <iostream>
int main(int argc, char** argv) {
std::string str("HelloWorld;");
breakStringAtCaps(str);
std::cout << str.c_str() << std::endl;
return 0;
}
and in your code,
string newname[10];
here 'newname' is a string array's name. you should not assign the name of array to a string instance.
and newname[i] means the i-th string of the array, not the i-th char of a string named 'newname' as you desired.

Find file with wildcards on entire disk

I need to find a variable extension file using wildcards (*.txt, *.jpeg, *.doc) on a disk (c:).
I tried with the following function, but I can't make it recursive, and it's not working:
void Recurse(LPCTSTR str)
{
WIN32_FIND_DATA file;
cout << "Value INPUT:" << str <<"\n";
HANDLE search_handle=FindFirstFile(str,&file);
if (search_handle) {
do {
if(file.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY &&
strcmp(file.cFileName,"..")!=0
&& strcmp(file.cFileName,".")!=0
&& strcmp(file.cFileName,"$Recycle.Bin")!=0
&& strcmp(file.cFileName,"Recovery")!=0
&& strcmp(file.cFileName,"System Volume Information")!=0
&& strcmp(file.cFileName,"PerfLogs")!=0
&& strcmp(file.cFileName,"Windows")!=0)
{
str = file.cFileName;
cout << "Directory :" << file.cFileName <<"\n";
Recurse(str); // Function to remind new directory
} else {
if (file.cFileName == "*.txt") {
std::wcout << file.cFileName << std::endl; //Print files find with estension txt
}
}
} while(FindNextFile(search_handle,&file));
CloseHandle(search_handle);
}
}
Of course I tried it with a static value (*.txt) to see if it worked, but it didn't work when I called it from in main:
Recurse("C:\\*");
I modified the code in this way. If it finds them throwing me * .txt files and me brings them correctly, but the path instead in many files shows me incorrect with several strange characters. I can not understand why such behavior.
char *my1;
char *my2;
char *my3;
char *my4;
char *cp2;
LPCTSTR ori;
std::string stringa_mia;
std::vector<std::string> listafiles;
std::vector<std::string> listapath;
void Recurse(LPCTSTR str, LPCTSTR ori)
{
WIN32_FIND_DATA file;
cp2 = strdup(str);
strcat(cp2, "\\*");
HANDLE search_handle=FindFirstFile(cp2,&file);
BOOL res = TRUE;
if (search_handle)
{
while(search_handle != INVALID_HANDLE_VALUE && res)
{
if(file.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY &&
strcmp(file.cFileName,"..")!=0
&& strcmp(file.cFileName,".")!=0
&& strcmp(file.cFileName,"$Recycle.Bin")!=0
&& strcmp(file.cFileName,"Recovery")!=0
&& strcmp(file.cFileName,"System Volume Information")!=0
&& strcmp(file.cFileName,"PerfLogs")!=0
&& strcmp(file.cFileName,"Windows")!=0
&& strcmp(file.cFileName,"Program Files")!=0
&& strcmp(file.cFileName,"Boot")!=0
&& strcmp(file.cFileName,"Program Files (x86)")!=0
&& strcmp(file.cFileName,"AppData")!=0
&& strcmp(file.cFileName,"ProgramData")!=0
&& strcmp(file.cFileName,"Cookies")!=0
&& strcmp(file.cFileName,".android")!=0
&& strcmp(file.cFileName,"Programmi")!=0
)
{
my3 = strdup(file.cFileName);
my2 = strdup(str);
my1 = strdup(ori);
strcat(my2, "\\");
strcat(my2, my3);
CloseHandle(search_handle);
Recurse(my2,ori);
}
else
{
my4 = strdup(file.cFileName);
stringa_mia = std::string(my4);
if (stringa_mia.find(".txt")!=std::string::npos) {
listafiles.push_back(stringa_mia);
listapath.push_back(str);
}
}
res = FindNextFile(search_handle,&file);
}
CloseHandle(search_handle);
}
}
i call the function in this mode:
Recurse("C:","C:");

Using a loop with std::strcmp to load lots of settings

In my game I keep track of unlocked levels with a vector std::vector<bool> lvlUnlocked_;.
The simple function to save the progress is this:
void save() {
std::stringstream ss;
std::string stringToSave = "";
std::ofstream ofile("./progress.txt");
if (ofile.good()) {
ofile.clear();
for (std::size_t i = 0; i < levelUnlocked_.size(); ++i) {
ss << "lvl" << i << "=" << (lvlUnlocked_.at(i) ? "1" : "0") << std::endl;
}
stringToSave = ss.str();
ofile << stringToSave;
ofile.close();
}
}
This works and is nice since I can just use a loop to dump the info.
Now to the part where I am stuck, the lower part of my load function (see comment in code below):
void load() {
std::ifstream ifile("./progress.txt");
if (ifile.good()) {
int begin;
int end;
std::string line;
std::string stringKey = "";
std::string stringValue = "";
unsigned int result;
while (std::getline(ifile, line)) {
stringKey = "";
stringValue = "";
for (unsigned int i = 0; i < line.length(); i++) {
if (line.at(i) == '=') {
begin = i + 1;
end = line.length();
break;
}
}
for (int i = 0; i < begin - 1; i++) {
stringKey += line.at(i);
}
for (int i = begin; i < end; i++) {
stringValue += line.at(i);
}
result = static_cast<unsigned int>(std::stoi(stringValue));
// usually I now compare the value and act accordingly, like so:
if (std::strcmp(stringKey.c_str(), "lvl0") == 0) {
lvlUnlocked_.at(0) = true;
} else if (std::strcmp(stringKey.c_str(), "lvl1") == 0) {
lvlUnlocked_.at(1) = true;
} else if (std::strcmp(stringKey.c_str(), "lvl2") == 0) {
lvlUnlocked_.at(2) = true;
}
// etc....
}
}
}
This works fine, but...
the problem is that I have 100+ levels and I want it to be dynamic based on the size of my lvlUnlocked_ vector instead of having to type it all like in the code above.
Is there a way to somehow make use of a loop like in my save function to check all levels?
If you parse your key to extract a suitable integer value, you can just index into the bit-vector with that:
while (std::getline(ifile, line)) {
const size_t eq = line.find('=');
if (eq == std::string::npos)
// no equals sign
continue;
auto stringKey = line.substr(0, eq);
auto stringValue = line.substr(eq+1);
if (stringKey.substr(0,3) != "lvl")
// doesn't begin with lvl
continue;
// strip off "lvl"
stringKey = stringKey.substr(3);
size_t end;
std::vector<bool>::size_type index = std::stoi(stringKey, &end);
if (end == 0 || end != stringKey.length())
// not a valid level number
continue;
if (index >= lvlUnlocked_.size())
// out of range
continue;
// Set it :-)
lvlUnlocked_[index] = stringValue=="1";
}
(I've also updated your parsing for "key=value" strings to more idiomatic C++.)

How to get string from xml in COM

I have one array like this:
static WCHAR FilesToShow[][100] = { { L"start.cmd" },{ L"image.xml" }, { L"xyz" }};
as you see that there is "xyz" which I have to replace with some unique name. For this I have to read image.xml file.
Please can you tell me how can I do this.
I wrote a method like this:
PRIVATE WCHAR GetSystemName(WCHAR *pName)
{
WCHAR line;
wfstream in("image.xml");
WCHAR tmp;
bool begin_tag = false;
while (getline(in,line))
{
// strip whitespaces from the beginning
for (int i = 0; i < line.length(); i++)
{
if (line[i] == ' ' && tmp.size() == 0)
{
}
else
{
tmp += line[i];
}
}
if (wcswcs(tmp,"<SystemPath>") != NULL)
{
???????? how to get "vikash" from here <SystemPath>C:\Users\rs_user\Documents\RobotStudio\Systems\vikash</SystemPath>
}
else
{
continue;
}
}
return tmp;
}
I'm getting exception for wfstream, getline and line.length() method.
I have included fstream.h header file but I think It's not supported in COM.
Please help me how to solve this issue without parsing xml file.
If your xml-file is simple enough so that there is only a single tag with given name, you could do it like this:
#include <string>
#include <sstream>
#include <iostream>
std::wstring get_value(std::wistream & in, std::wstring const & tagname)
{
std::wstring text = std::wstring(std::istreambuf_iterator<std::wstring::value_type>(in),
std::istreambuf_iterator<std::wstring::value_type>());
std::wstring start_tag = L"<" + tagname + L">";
std::wstring end_tag = L"</" + tagname + L">";
std::wstring::size_type start = text.find(start_tag);
if (start == std::wstring::npos)
{
throw 123;
}
start += start_tag.length();
std::wstring::size_type end = text.find(end_tag);
if (end == std::wstring::npos)
{
throw 123;
}
return text.substr(start, end - start);
}
std::wstring get_substr_after(std::wstring const & str, wchar_t delim)
{
std::wstring::size_type pos = str.rfind(delim);
if (pos == std::wstring::npos)
{
throw 123;
}
return str.substr(pos + 1);
}
void stackoverflow()
{
std::wstring text(L"<foo>\n<bar>abc/def/ghi</bar>\n<baz>123/456/789</baz>\n</foo>\n");
std::wistringstream wiss(text);
std::wcout << text << std::endl;
std::wcout << get_substr_after(get_value(wiss, std::wstring(L"bar")), L'/') << std::endl;
}
The output of this program is:
<foo>
<bar>abc/def/ghi</bar>
<baz>123/456/789</baz>
</foo>
ghi
I hope that answered your question.
you have several issues here.
what you are getting are compiler errors and not exceptions
the header file to include is 'fstream' not 'fstream.h'.
make sure you have a line saying using namespace std;
You are declaring line as a variable of type WCHAR, so it is a single wide character, which surely is not a wstring object. Therefore line.length() is incorrect.
Why are you mixing C (wcswcs()) and C++ (STL) ? maybe you should re-design your function signature.
However, try the below function. I have modified the signature to return a pointer to WCHAR, and place the requested string in the buffer space provided by pName. I added a check to verify that the buffer is large enough to fit the name and the terminating NULL character.
WCHAR* GetSystemName(WCHAR *pName, size_t buflen)
{
wstring line;
wifstream in("image.xml");
WCHAR* tmp = NULL;
while (getline(in,line))
{
// strip whitespaces from the beginning
size_t beg_non_whitespace = line.find_first_not_of(L" \t");
if (beg_non_whitespace != wstring::npos)
{
line = line.substr( beg_non_whitespace );
}
size_t beg_system_path = line.find( L"<SystemPath>" );
if ( beg_system_path != wstring::npos )
{
// strip the tags (assuming closing tag is present)
size_t beg_data = beg_system_path + wstring( L"<SystemPath>" ).length();
size_t range = line.find( L"</SystemPath>" ) - beg_data;
line = line.substr( beg_data, range );
// get file name
size_t pos_last_backslash = line.find_last_of( L'\\' );
if ( pos_last_backslash != wstring::npos )
{
line = line.substr( pos_last_backslash + 1 );
if ( buflen <= line.length() )
{
// ERROR: pName buffer is not large enough to fit the string + terminating NULL character.
return NULL;
}
wcscpy( pName, line.c_str() );
tmp = pName;
break;
}
}
}
return tmp;
}
EDIT: Moreover, if you are using and/or parsing XML in other areas of your program, I strongly suggest using an XML parsing library such as Xerces-C or libXml2.
Thank you all for your answer. Here I got solution of my question.
PRIVATE WCHAR* GetNewSystemName()
{
WCHAR line[756];
WCHAR tempBuffer[100];
CComBSTR path = CurrentFolder.Path();
CComBSTR imagePath1 = L"rimageinfo.xml";
path.AppendBSTR(imagePath1);
std::wfstream in(path);
WCHAR tmp[756];
in.getline(line, 756);
WCHAR* buffer;
buffer = wcswcs(line, L"<SystemPath>");
WCHAR *dest = wcsstr(buffer, L"</SystemPath>");
int pos;
pos = dest - buffer;
unsigned int i = 0;
if (wcswcs(buffer,L"<SystemPath>") != NULL && wcswcs(buffer,L"</SystemPath>") != NULL)
{
for (; i < pos; i++)
{
if (buffer[i] == ' ' && sizeof(tmp) == 0)
{
}
else
{
tmp[i] = buffer[i];
}
}
tmp[i] = NULL;
//break;
}
int j = i;
for (; j > 0; j--)
{
if (tmp[j] == '\\')
{
break;
}
}
j++;
int k = 0;
for (; j < i ; j++)
{
System_Name[k] = tmp[j];
k++;
}
System_Name[k] = NULL;
return System_Name;