CFile %&%
-
Hello, this: CFileDialog dlg(FALSE); CString fname = dlg.GetPathName(); const char* szBuffer = fname; int i = sizeof(szBuffer); CFile f; if( f.Open ("F:\\test\\id.txt", CFile::modeWrite ) ){ try{ f.Write(szBuffer, i); f.Close(); } catch (CFileException *e){ AfxMessageBox ("Error!"); e->Delete(); } } } ...if you type in "rtf" it insert in my "id.txt" file: F:\r¼ÖA Q a ¼ºÜþ4 ýýýý€*0 *0 ¬)0 Ì(0 ÍÍÍÍÍÍÍÍØ(0 why?? Thanks, Mark
-
Hello, this: CFileDialog dlg(FALSE); CString fname = dlg.GetPathName(); const char* szBuffer = fname; int i = sizeof(szBuffer); CFile f; if( f.Open ("F:\\test\\id.txt", CFile::modeWrite ) ){ try{ f.Write(szBuffer, i); f.Close(); } catch (CFileException *e){ AfxMessageBox ("Error!"); e->Delete(); } } } ...if you type in "rtf" it insert in my "id.txt" file: F:\r¼ÖA Q a ¼ºÜþ4 ýýýý€*0 *0 ¬)0 Ì(0 ÍÍÍÍÍÍÍÍØ(0 why?? Thanks, Mark
You might want to try:
CFileDialog dlg(FALSE); CString fname = dlg.GetPathName(); const char* szBuffer = fname.GetBuffer(0); int i = fname.GetLength(); CFile f; // CFile::modeWrite is OK if you want to append // data to the end of the file. If you're looking // to create a new file and have it contain JUST this // filename, you need to open the CFile with the // CFile::modeCreate flag. if( f.Open ("F:\\test\\id.txt", CFile::modeWrite ) ) { try{ f.Write(szBuffer, i); f.Close(); } catch (CFileException *e) { AfxMessageBox ("Error!"); e->Delete(); } } // And remember to release your string pointer. fname.ReleaseBuffer();
-Mike Zinni "No shit it's tough. If it wasn't, everybody and their sister would be an engineer and then you wouldn't have a job." -
Hello, this: CFileDialog dlg(FALSE); CString fname = dlg.GetPathName(); const char* szBuffer = fname; int i = sizeof(szBuffer); CFile f; if( f.Open ("F:\\test\\id.txt", CFile::modeWrite ) ){ try{ f.Write(szBuffer, i); f.Close(); } catch (CFileException *e){ AfxMessageBox ("Error!"); e->Delete(); } } } ...if you type in "rtf" it insert in my "id.txt" file: F:\r¼ÖA Q a ¼ºÜþ4 ýýýý€*0 *0 ¬)0 Ì(0 ÍÍÍÍÍÍÍÍØ(0 why?? Thanks, Mark
-
You might want to try:
CFileDialog dlg(FALSE); CString fname = dlg.GetPathName(); const char* szBuffer = fname.GetBuffer(0); int i = fname.GetLength(); CFile f; // CFile::modeWrite is OK if you want to append // data to the end of the file. If you're looking // to create a new file and have it contain JUST this // filename, you need to open the CFile with the // CFile::modeCreate flag. if( f.Open ("F:\\test\\id.txt", CFile::modeWrite ) ) { try{ f.Write(szBuffer, i); f.Close(); } catch (CFileException *e) { AfxMessageBox ("Error!"); e->Delete(); } } // And remember to release your string pointer. fname.ReleaseBuffer();
-Mike Zinni "No shit it's tough. If it wasn't, everybody and their sister would be an engineer and then you wouldn't have a job." -
You might want to try:
CFileDialog dlg(FALSE); CString fname = dlg.GetPathName(); const char* szBuffer = fname.GetBuffer(0); int i = fname.GetLength(); CFile f; // CFile::modeWrite is OK if you want to append // data to the end of the file. If you're looking // to create a new file and have it contain JUST this // filename, you need to open the CFile with the // CFile::modeCreate flag. if( f.Open ("F:\\test\\id.txt", CFile::modeWrite ) ) { try{ f.Write(szBuffer, i); f.Close(); } catch (CFileException *e) { AfxMessageBox ("Error!"); e->Delete(); } } // And remember to release your string pointer. fname.ReleaseBuffer();
-Mike Zinni "No shit it's tough. If it wasn't, everybody and their sister would be an engineer and then you wouldn't have a job." -
Try:
CFileDialog dlg(FALSE); CString fname = dlg.GetPathName(); 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 file data. szBuffer = new char[i]; // Read in the file data if(szBuffer != NULL) f.Read(szBuffer, i); // Close the file. f.Close(); } catch (CFileException *e) { AfxMessageBox ("Error!"); e->Delete(); } // end TRY-CATCH } // end IF // If szBuffer != NULL, it contains the file data. // Do whatever you want to it, and then make sure you // clean up the dynamic memory you new'ed. if(szBuffer != NULL) delete [] szBuffer;
-Mike Zinni "No shit it's tough. If it wasn't, everybody and their sister would be an engineer and then you wouldn't have a job."
-
CFileDialog dlg(FALSE); dlg.DoModal(); CString fname = dlg.GetPathName(); const char\* szBuffer = fname; char s\[128\]; int i = lstrlen(szBuffer); CFile f; if (f.Open("F:\\\\test\\\\id.txt", CFile::modeWrite | CFile::modeCreate) == TRUE) { try { f.Write(szBuffer, i); } catch (CFileException \*e) { AfxMessageBox ("Error writing!"); e->Delete(); } f.Close(); } if (f.Open("F:\\\\test\\\\id.txt", CFile::modeRead) == TRUE) { try { f.Read(s, i); } catch (CFileException \*e) { AfxMessageBox ("Error reading!"); e->Delete(); } }
-
Try:
CFileDialog dlg(FALSE); CString fname = dlg.GetPathName(); 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 file data. szBuffer = new char[i]; // Read in the file data if(szBuffer != NULL) f.Read(szBuffer, i); // Close the file. f.Close(); } catch (CFileException *e) { AfxMessageBox ("Error!"); e->Delete(); } // end TRY-CATCH } // end IF // If szBuffer != NULL, it contains the file data. // Do whatever you want to it, and then make sure you // clean up the dynamic memory you new'ed. if(szBuffer != NULL) delete [] szBuffer;
-Mike Zinni "No shit it's tough. If it wasn't, everybody and their sister would be an engineer and then you wouldn't have a job."
-
I must cast the szBuffer to a CString type: CString csPath = szBuffer; is this allowed? Thanks, Mark
Actually, this implicitly casts szBuffer to a LPCTSTR, which CString has an assignment operator for.
-
Try:
CFileDialog dlg(FALSE); CString fname = dlg.GetPathName(); 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 file data. szBuffer = new char[i]; // Read in the file data if(szBuffer != NULL) f.Read(szBuffer, i); // Close the file. f.Close(); } catch (CFileException *e) { AfxMessageBox ("Error!"); e->Delete(); } // end TRY-CATCH } // end IF // If szBuffer != NULL, it contains the file data. // Do whatever you want to it, and then make sure you // clean up the dynamic memory you new'ed. if(szBuffer != NULL) delete [] szBuffer;
-Mike Zinni "No shit it's tough. If it wasn't, everybody and their sister would be an engineer and then you wouldn't have a job."