Well, the subject tells everything I want to do, I've made a functions to convert both a std::string to a LPCSTR, and a function to convert a std::string to a wchar_t*. However, both of them don't work.
these are the functions:
LPCSTR shtrIO::ConvertStrToLPCSTR(std::string stringVar)
{
LPCSTR lpcstrReturn;
lpcstrReturn = stringVar.c_str();
return lpcstrReturn;
}
wchar_t* shtrIO::ConvertStrToWChar(std::string stringVar)
{
const char *tempChar = stringVar.c_str();
size_t origsize = strlen(tempChar) + 1;
const size_t newsize = 100;
size_t convertedChars = 0;
wchar_t wcstring[newsize];
mbstowcs_s(&convertedChars, wcstring, origsize, tempChar, _TRUNCATE);
wcscat_s(wcstring, L"(wchar_t *)");
return wcstring;
}
I use them as following, first import a main file that defines where all the game files are:
void shtrIO::InputInitialization()
{
std::string initFileLine;
std::ifstream initFile;
initFile.open("./Data/main.txt");
if (initFile.is_open()) {
getline (initFile, initFileLine);
if (initFileLine == "ENDFILE") {
initFile.close();
return;
}
if (initFileLine == "BEGINTERRAIN") {
getline (initFile, initFileLine);
while (initFileLine != "ENDTERRAIN") {
//std::string tempString = initFileLine;
//wchar_t passedValue;
//ConvertStrToWChar(tempString, passedValue);
shtrGameClass.TerrainMembers[numImportedTerrains].TerrainLocation = ConvertStrToWChar(initFileLine);
getline (initFile, initFileLine);
numImportedTerrains++;
}
}
getline (initFile, initFileLine);
if (initFileLine == "ENDFILE") {
initFile.close();
return;
}
} else {
MessageBox(0, "Error opening main file", "Error", MB_OK);
PostQuitMessage(0);
}
}
Problem 1:
Error Message: warning C4172: returning address of local variable or temporary
I know what the error message means, but I haven't got an idea on how to fix it.
As you can see, the program imports the line "./Data/Terrain/terrain.txt" and stores it in shtrGameClass.TerrainMembers[numImportedTerrains].TerrainLocation (numImportedTerrains is a var I'm going to use in the future, right now, it's allways 0).
However the following happens:
before storing it, the value of initFileLine = "./Data/Terrain/terrain.txt"
after going through the ConvertStrToWChar function, but BEFORE returning wcstring, the wchar_t* = "./Data/Terrain/terrain.txt(wchar_t)"
after the function ends, the value in the terrainLocation var = "./Data/Terrain/terrain.txt(wchar_t)"
but when I get a new line from the main.txt file (the new line = ENDTERRAIN), the value get scrambled up and goes to: "./Data/Ter(chinees signs)"
How do I keep the value "./Data/Terrain/terrain.txt(wchar_t)"?
Problem 2:
Lets assume the file was opened correctly by the second function, here below (by replacing shtrGameClass.TerrainMembers[0].TerrainLocation with the actual file):
void shtrIO::InputTerrain()
{
std::ifstream terrainFile;
std::string terrainFileLine;
terrainFile.open(shtrGameClass.TerrainMembers[0].TerrainLocation);
int timesXVarPassed;
int numTextures = 0;
if (terrainFile.is_open()) {
timesXVarPassed = 0;
getline(terrainFile, terrainFileLine);
if (terrainFileLine == "BEGINTEXTURES") {
getline(terrainFile, terrainFileLine);
while (terrainFileLine != "ENDTEXTURES") {
shtrGameClass.TerrainMembers[0].textures[numTextures].textureNumber = numTextures;
shtrGameClass.TerrainMembers[0].textures[numTextures].textureFile = ConvertStrToLPCSTR(terrainFileLine);
numTextures++;
getline(terrainFile, terrainFileLine);
}
getline(terrainFile, terrainFileLine);
} else {
MessageBox(0, "No textures in terrain file", "Error in terrain", MB_OK);
PostQuitMessage(0);
}
if (terrainFileLine == "BEGINDESC") {
getline(terrainFile, terrainFileLine);
shtrGameClass.TerrainMembers[0].xSize = ConvertStrToInt(terrainFileLine);
getline(terrainFile, terrainFileLine);
shtrGameClass.TerrainMembers[0].ySize = ConvertStrToInt(terrainFileLine);
getline(terrainFile, terrainFileLine);
shtrGameClass.TerrainMembers[0].tileXSize = (float)ConvertStrToInt(terrainFileLine);
getline(terrainFile, terrainFileLine);
shtrGameClass.TerrainMembers[0].tileYSize = (float)ConvertStrToInt(terrainFileLine);
getline(terrainFile, terrainFileLine);
if (terrainFileLine == "ENDDESC") {
getline(terrainFile, terrainFileLine);
} else {
MessageBox(0, "TerrainDesc should have 4 values", "Error in terrain", MB_OK);
PostQuitMessage(0);
}
}
if (terrainFileLine == "BEGINTERRAIN") {
while (terrainFileLine != "ENDTERRAIN") {
for (int xVar = 0; xVar < shtrGameClass.TerrainMembers[0].xSize; xVar++) {
for (int yVar = 0; yVar < shtrGameClass.TerrainMembers[0].ySize; yVar++) {
getline(terrainFile, terrainFileLine);
shtrGameClass.TerrainMembers[0].TerrainLayout[xVar][yVar] = ConvertStrToInt(terrainFileLine);
}
}
timesXVarPassed++;
}
} else {
MessageBox(0, "No terrain body in terrain file", "Error in terrain", MB_OK);
PostQuitMessage(0);
}
} else {
MessageBox(0, "Error opening terrain file", "Error in terrain", MB_OK);
}
shtrGameClass.numTerrainFiles = 1;
terrainFile.close();
}
It loops through the entire file, correctly storing the xsize, ysize, texture number etc. However, upon trying to store the texture path (LPCSTR for ease of use in the CreateTexture2D function), the following happens:
before passing terrainFileLine to the ConvertStrToLPCSTR function, the value is: "./Data/Textures/terrain1.dds"
Before returning lpcstrReturn to the texturefile var, lpcstrReturn = "./Data/Textures/terrain1.dds"
After 'closing' the function, the lpcstrReturn value suddenly turns to: "îþîþîþîþîþî" (repeating)
This value also gets stored in the textureFile variable.
I think I gave all the information needed, could somebody help me out here?