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. CFile

CFile

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
5 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.
  • M Offline
    M Offline
    macmac38
    wrote on last edited by
    #1

    Why this generates "test.pmjaaaaYYYYY" as output?? char* szBuffer = NULL; int i = 0; CFile f; if( f.Open ("F:\\test\\id.txt", CFile::modeRead ) ){ try { // Get the size of the file. i = f.GetLength(); // Create buffer to hold filedata. szBuffer = new char[i]; // Read in the file data if(szBuffer != NULL) f.Read(szBuffer, i); f.Close(); // Close the file. } catch (CFileException *e) { AfxMessageBox ("Error!"); e->Delete(); } // end TRY-CATCH } // end IF CString csPath = szBuffer; SaveStream(csPath); if(szBuffer != NULL) delete [] szBuffer; Thanks, Mark

    N H 2 Replies Last reply
    0
    • M macmac38

      Why this generates "test.pmjaaaaYYYYY" as output?? char* szBuffer = NULL; int i = 0; CFile f; if( f.Open ("F:\\test\\id.txt", CFile::modeRead ) ){ try { // Get the size of the file. i = f.GetLength(); // Create buffer to hold filedata. szBuffer = new char[i]; // Read in the file data if(szBuffer != NULL) f.Read(szBuffer, i); f.Close(); // Close the file. } catch (CFileException *e) { AfxMessageBox ("Error!"); e->Delete(); } // end TRY-CATCH } // end IF CString csPath = szBuffer; SaveStream(csPath); if(szBuffer != NULL) delete [] szBuffer; Thanks, Mark

      N Offline
      N Offline
      Neville Franks
      wrote on last edited by
      #2

      The file you are reading won't have a 0 terminator. Try: szBuffer = new char[i+1]; // Read in the file data szBuffer[i] = 0; Neville Franks, Author of ED for Windows. www.getsoft.com Make money with our new Affilate program

      M 1 Reply Last reply
      0
      • N Neville Franks

        The file you are reading won't have a 0 terminator. Try: szBuffer = new char[i+1]; // Read in the file data szBuffer[i] = 0; Neville Franks, Author of ED for Windows. www.getsoft.com Make money with our new Affilate program

        M Offline
        M Offline
        macmac38
        wrote on last edited by
        #3

        Now it works. Will a "Detected memory leaks!" be a problem? Thanks, Mark

        N 1 Reply Last reply
        0
        • M macmac38

          Why this generates "test.pmjaaaaYYYYY" as output?? char* szBuffer = NULL; int i = 0; CFile f; if( f.Open ("F:\\test\\id.txt", CFile::modeRead ) ){ try { // Get the size of the file. i = f.GetLength(); // Create buffer to hold filedata. szBuffer = new char[i]; // Read in the file data if(szBuffer != NULL) f.Read(szBuffer, i); f.Close(); // Close the file. } catch (CFileException *e) { AfxMessageBox ("Error!"); e->Delete(); } // end TRY-CATCH } // end IF CString csPath = szBuffer; SaveStream(csPath); if(szBuffer != NULL) delete [] szBuffer; Thanks, Mark

          H Offline
          H Offline
          Hari Krishnan Noida
          wrote on last edited by
          #4

          Hi, As, franks mentioned, it happens because, the output is not null-terminated. Here's the code snippet, which very well read your file, without any memory leaks. ----------------------------------------------------------- CFile oFile; DWORD dwFileLength; if(oFile.Open("F:\\test\\id.txt", CFile::modeRead)) { try { CString strBuffer; dwFileLength = oFile.GetLength(); LPTSTR szBuffer = strBuffer.GetBufferSetLength(dwFileLength); oFile.Read((LPVOID)szBuffer, dwFileLength); oFile.Close(); CString csPath(strBuffer); strBuffer.ReleaseBuffer(); } catch(CFileException *e) { AfxMessageBox ("Error!"); e->Delete(); } } ----------------------------------------------------------- regards ~Hari~

          1 Reply Last reply
          0
          • M macmac38

            Now it works. Will a "Detected memory leaks!" be a problem? Thanks, Mark

            N Offline
            N Offline
            Neville Franks
            wrote on last edited by
            #5

            macmac38 wrote: Will a "Detected memory leaks!" be a problem? Short answer, Yes. Also if you are reading a fixed length file, you should write using fixed length routines, not a stream. ie, don't mix file i/o types unless you have to. Neville Franks, Author of ED for Windows. www.getsoft.com Make money with our new Affilate program

            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