CFile
-
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 -
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, MarkThe 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 -
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 -
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, MarkHi, 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~
-
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