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
10 Posts 4 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

    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

    M H 2 Replies Last reply
    0
    • M macmac38

      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

      M Offline
      M Offline
      Mike Zinni
      wrote on last edited by
      #2

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

      M 2 Replies Last reply
      0
      • M macmac38

        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

        H Offline
        H Offline
        Hans Ruck
        wrote on last edited by
        #3

        int i = strlen(szBuffer); rechi

        1 Reply Last reply
        0
        • M Mike Zinni

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

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

          It works!!!!Thanx. Thanks, Mark

          1 Reply Last reply
          0
          • M Mike Zinni

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

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

            By the way, how can i change the code to read the file? Thanks, Mark

            M D 2 Replies Last reply
            0
            • M macmac38

              By the way, how can i change the code to read the file? Thanks, Mark

              M Offline
              M Offline
              Mike Zinni
              wrote on last edited by
              #6

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

              M 2 Replies Last reply
              0
              • M macmac38

                By the way, how can i change the code to read the file? Thanks, Mark

                D Offline
                D Offline
                David Crow
                wrote on last edited by
                #7
                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();
                    }
                }
                
                1 Reply Last reply
                0
                • M Mike Zinni

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

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

                  I must cast the szBuffer to a CString type: CString csPath = szBuffer; is this allowed? Thanks, Mark

                  D 1 Reply Last reply
                  0
                  • M macmac38

                    I must cast the szBuffer to a CString type: CString csPath = szBuffer; is this allowed? Thanks, Mark

                    D Offline
                    D Offline
                    David Crow
                    wrote on last edited by
                    #9

                    Actually, this implicitly casts szBuffer to a LPCTSTR, which CString has an assignment operator for.

                    1 Reply Last reply
                    0
                    • M Mike Zinni

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

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

                      Nice, but still generates test.pmjyyyxx as output???? Thanks, Mark

                      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