Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. ofstream optimization problem

ofstream optimization problem

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestionalgorithmsperformancetutorial
15 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    doug25
    wrote on last edited by
    #1

    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?

    L 1 Reply Last reply
    0
    • D doug25

      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?

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Unfortunately you don't. The wstream types take wide characters in but write them out as normal ASCII. You should use the old C-style fwrite or the newer WriteFile functions which allow you to specify the output character size.

      Use the best guess

      D 1 Reply Last reply
      0
      • L Lost User

        Unfortunately you don't. The wstream types take wide characters in but write them out as normal ASCII. You should use the old C-style fwrite or the newer WriteFile functions which allow you to specify the output character size.

        Use the best guess

        D Offline
        D Offline
        doug25
        wrote on last edited by
        #3

        so wofstream can't be optimized? it all works well without optimization

        L 1 Reply Last reply
        0
        • D doug25

          so wofstream can't be optimized? it all works well without optimization

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          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

          D 1 Reply Last reply
          0
          • L Lost User

            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

            D Offline
            D Offline
            doug25
            wrote on last edited by
            #5

            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.

            L 1 Reply Last reply
            0
            • D doug25

              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.

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              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

              D 1 Reply Last reply
              0
              • L Lost User

                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

                D Offline
                D Offline
                doug25
                wrote on last edited by
                #7

                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)

                L J 2 Replies Last reply
                0
                • D doug25

                  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)

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  What problem and where does it occur?

                  Use the best guess

                  D 1 Reply Last reply
                  0
                  • D doug25

                    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)

                    J Offline
                    J Offline
                    Jochen Arndt
                    wrote on last edited by
                    #9

                    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.

                    D 1 Reply Last reply
                    0
                    • L Lost User

                      What problem and where does it occur?

                      Use the best guess

                      D Offline
                      D Offline
                      doug25
                      wrote on last edited by
                      #10

                      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.

                      L 1 Reply Last reply
                      0
                      • J Jochen Arndt

                        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.

                        D Offline
                        D Offline
                        doug25
                        wrote on last edited by
                        #11

                        thanks, I'll try that

                        1 Reply Last reply
                        0
                        • D doug25

                          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.

                          L Offline
                          L Offline
                          Lost User
                          wrote on last edited by
                          #12

                          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

                          D 2 Replies Last reply
                          0
                          • L Lost User

                            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

                            D Offline
                            D Offline
                            doug25
                            wrote on last edited by
                            #13

                            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.

                            1 Reply Last reply
                            0
                            • L Lost User

                              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

                              D Offline
                              D Offline
                              doug25
                              wrote on last edited by
                              #14

                              how do I close a thread?

                              L 1 Reply Last reply
                              0
                              • D doug25

                                how do I close a thread?

                                L Offline
                                L Offline
                                Lost User
                                wrote on last edited by
                                #15

                                You can just stop posting, or edit your original question and add "[closed]" or "[solved]" to the title, and a comment to the effect that you have closed it.

                                Use the best guess

                                1 Reply Last reply
                                0
                                Reply
                                • Reply as topic
                                Log in to reply
                                • Oldest to Newest
                                • Newest to Oldest
                                • Most Votes


                                • Login

                                • Don't have an account? Register

                                • Login or register to search.
                                • First post
                                  Last post
                                0
                                • Categories
                                • Recent
                                • Tags
                                • Popular
                                • World
                                • Users
                                • Groups