ofstream optimization problem
-
Hi, I'm using wofstream to write text to a file my problem is that, with any level of optimization, neither write() or << allows me to write a wstring or c string in the form of (WCHAR*). example
WCHAR* str = new WCHAR[MAX_PATH];
wcscpy(str, L"walk");
myfile.write(str, wcslen(str));No string written to file
wstring str2 = L"walk";
myfile << str2;No string written to file I am able to to do
myfile << "String";
how do I fix?
-
Hi, I'm using wofstream to write text to a file my problem is that, with any level of optimization, neither write() or << allows me to write a wstring or c string in the form of (WCHAR*). example
WCHAR* str = new WCHAR[MAX_PATH];
wcscpy(str, L"walk");
myfile.write(str, wcslen(str));No string written to file
wstring str2 = L"walk";
myfile << str2;No string written to file I am able to to do
myfile << "String";
how do I fix?
-
Unfortunately you don't. The
wstream
types take wide characters in but write them out as normal ASCII. You should use the old C-stylefwrite
or the newerWriteFile
functions which allow you to specify the output character size.Use the best guess
-
doug25 wrote:
so wofstream can't be optimized? it all works well without optimization
Sorry, I don't understand what you are saying here. As far as Irecall from all the tests I have run, you cannot write wide characters via a wstream. I would be interested to see the code you have that does work. In the meantime I will retry my own tests.
Use the best guess
-
doug25 wrote:
so wofstream can't be optimized? it all works well without optimization
Sorry, I don't understand what you are saying here. As far as Irecall from all the tests I have run, you cannot write wide characters via a wstream. I would be interested to see the code you have that does work. In the meantime I will retry my own tests.
Use the best guess
sorry, I've discovered what was happening, it isn't a problem with wstring optimazation. I was converting the file to another format the functions i'm using behave differently with optimization. The problem is nothing to do with vectors or wstrings. it's a directx set of functions i'm using that fail when optimazation is turned on. It was difficult to determine tat this was where the program was failing.
-
sorry, I've discovered what was happening, it isn't a problem with wstring optimazation. I was converting the file to another format the functions i'm using behave differently with optimization. The problem is nothing to do with vectors or wstrings. it's a directx set of functions i'm using that fail when optimazation is turned on. It was difficult to determine tat this was where the program was failing.
-
doug25 wrote:
the functions i'm using behave differently with optimization.
I find that very difficult to believe. Much more likely is that you have a bug somewhere that only shows when you have optimization turned on.
Use the best guess
well, it could be a mistake I've made but I've managed to find exactly where the error occurs by using #pragma optimize("g", off) and #pragma optimize("g", on) it's one function, I can't see any mistakes
#pragma optimize("g", off)
void AddDataObject(ID3DXFileSaveObject* xFileSave, ID3DXFileData* child, ID3DXFileSaveData* xSaveData)
{
// Add data for this child then each of it's children
SIZE_T nChildren = 0;
child->GetChildren(&nChildren);// add child GUID templateid, id; child->GetType(&templateid); child->GetId(&id); char name\[MAX\_PATH\]; SIZE\_T sz; child->GetName(name, &sz); SIZE\_T dataSize; void\* data = NULL; child->Lock(&dataSize, (LPCVOID\*)&data); if(xSaveData) { xSaveData->AddDataObject(templateid, name, (const GUID\*)&id, dataSize, data, &xSaveData); } else { xFileSave->AddDataObject(templateid, name, (const GUID\*)&id, dataSize, data, &xSaveData); } child->Unlock(); // For each child of child, add child data // Add data for all sub children (recurse) for(int i=0; iGetChild(i, &ch))) { // Add data for children of this child (ch) AddDataObject(xFileSave, ch, xSaveData); } }
}
#pragma optimize("g", on)
-
well, it could be a mistake I've made but I've managed to find exactly where the error occurs by using #pragma optimize("g", off) and #pragma optimize("g", on) it's one function, I can't see any mistakes
#pragma optimize("g", off)
void AddDataObject(ID3DXFileSaveObject* xFileSave, ID3DXFileData* child, ID3DXFileSaveData* xSaveData)
{
// Add data for this child then each of it's children
SIZE_T nChildren = 0;
child->GetChildren(&nChildren);// add child GUID templateid, id; child->GetType(&templateid); child->GetId(&id); char name\[MAX\_PATH\]; SIZE\_T sz; child->GetName(name, &sz); SIZE\_T dataSize; void\* data = NULL; child->Lock(&dataSize, (LPCVOID\*)&data); if(xSaveData) { xSaveData->AddDataObject(templateid, name, (const GUID\*)&id, dataSize, data, &xSaveData); } else { xFileSave->AddDataObject(templateid, name, (const GUID\*)&id, dataSize, data, &xSaveData); } child->Unlock(); // For each child of child, add child data // Add data for all sub children (recurse) for(int i=0; iGetChild(i, &ch))) { // Add data for children of this child (ch) AddDataObject(xFileSave, ch, xSaveData); } }
}
#pragma optimize("g", on)
-
well, it could be a mistake I've made but I've managed to find exactly where the error occurs by using #pragma optimize("g", off) and #pragma optimize("g", on) it's one function, I can't see any mistakes
#pragma optimize("g", off)
void AddDataObject(ID3DXFileSaveObject* xFileSave, ID3DXFileData* child, ID3DXFileSaveData* xSaveData)
{
// Add data for this child then each of it's children
SIZE_T nChildren = 0;
child->GetChildren(&nChildren);// add child GUID templateid, id; child->GetType(&templateid); child->GetId(&id); char name\[MAX\_PATH\]; SIZE\_T sz; child->GetName(name, &sz); SIZE\_T dataSize; void\* data = NULL; child->Lock(&dataSize, (LPCVOID\*)&data); if(xSaveData) { xSaveData->AddDataObject(templateid, name, (const GUID\*)&id, dataSize, data, &xSaveData); } else { xFileSave->AddDataObject(templateid, name, (const GUID\*)&id, dataSize, data, &xSaveData); } child->Unlock(); // For each child of child, add child data // Add data for all sub children (recurse) for(int i=0; iGetChild(i, &ch))) { // Add data for children of this child (ch) AddDataObject(xFileSave, ch, xSaveData); } }
}
#pragma optimize("g", on)
That is a recursive function. You may generate assembler output to see the differences between the optimized and unoptimized code. You should also check for success on all functions that may fail. At least the
AddDataObject()
return values should be checked and returned upon errors to leave the recursion if something wrent wrong. -
the problem is some strings are not loaded, or at least are not written to the new file. e.g. each line in the output file that should contain "Frame frame_name {" (works without opt.) reads "Frame {". I will try freeing the memory of child data objects although I have assumed they get released when the top level data object get released and I will try what Jochan suggested. There is very little documentation on how to use these functions. I won't be able to reply for a while if I do fix the problem but cheers.
-
That is a recursive function. You may generate assembler output to see the differences between the optimized and unoptimized code. You should also check for success on all functions that may fail. At least the
AddDataObject()
return values should be checked and returned upon errors to leave the recursion if something wrent wrong. -
the problem is some strings are not loaded, or at least are not written to the new file. e.g. each line in the output file that should contain "Frame frame_name {" (works without opt.) reads "Frame {". I will try freeing the memory of child data objects although I have assumed they get released when the top level data object get released and I will try what Jochan suggested. There is very little documentation on how to use these functions. I won't be able to reply for a while if I do fix the problem but cheers.
doug25 wrote:
the problem is some strings are not loaded, or at least are not written to the new file. e.g. each line in the output file that should contain "Frame frame_name {" (works without opt.) reads "Frame {".
Now I'm totally lost; the code you posted does not seem to have anything at all to do with these comments. It may be better to close off this thread and start a new question when you are ready to proceed.
Use the best guess
-
doug25 wrote:
the problem is some strings are not loaded, or at least are not written to the new file. e.g. each line in the output file that should contain "Frame frame_name {" (works without opt.) reads "Frame {".
Now I'm totally lost; the code you posted does not seem to have anything at all to do with these comments. It may be better to close off this thread and start a new question when you are ready to proceed.
Use the best guess
I haven't explained it very well. Basically, I've written a program that writes a .x file using wofstream. But the file doesn't look very pretty so I'm using the directx code I posted to convert it to a better format, so I can also save it as binary or compressed as well as structured text. Basically saves me having to rewrite the program. I'll close the thread.
-
doug25 wrote:
the problem is some strings are not loaded, or at least are not written to the new file. e.g. each line in the output file that should contain "Frame frame_name {" (works without opt.) reads "Frame {".
Now I'm totally lost; the code you posted does not seem to have anything at all to do with these comments. It may be better to close off this thread and start a new question when you are ready to proceed.
Use the best guess