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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Writing Files

Writing Files

Scheduled Pinned Locked Moved C / C++ / MFC
c++htmlhelptutorialquestion
4 Posts 2 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.
  • O Offline
    O Offline
    orcblood
    wrote on last edited by
    #1

    I think thats what its called. Anyway, I got the open file dialog working. Since I am a lazy bumb I have decided to make the program compatible with all files (that have text). However, I do not know how I would wright the text from the file into the edit box. Could someone help me here? I mainly want it so that it can open a .html or .htm, .c or .cpp, .h, .txt files. How would I do this I have no idea. So could some one either tell me how (prefereable with some example code) or tell me a URL that might help me. Thanks, orcblood

    M 1 Reply Last reply
    0
    • O orcblood

      I think thats what its called. Anyway, I got the open file dialog working. Since I am a lazy bumb I have decided to make the program compatible with all files (that have text). However, I do not know how I would wright the text from the file into the edit box. Could someone help me here? I mainly want it so that it can open a .html or .htm, .c or .cpp, .h, .txt files. How would I do this I have no idea. So could some one either tell me how (prefereable with some example code) or tell me a URL that might help me. Thanks, orcblood

      M Offline
      M Offline
      MAAK
      wrote on last edited by
      #2

      If I understand you right, you want to read text from file and load it into and Edit control. If it is so then this is sample code for both reading both ANSI and UNICODE text files:

             CFile f;
             if(f.open("FILENAME.TXT, CFile::modeRead))
             {
                 DWORD dwFLen = f.GetLength();
                 //this short will be used to get the file header
                 USHORT usHeader;
                 f.Read(&usHeader, sizeof(USHORT));
      
                 //UNICODE text files starts with value 0xFEFF
                 if(usHeader == 0xFEFF)
                 {
                     //we are going to load the text into a character array
                     wchar_t * filetxt;
                     
                     //allocate a charaters array one for null terminator
                     filetxt = new wchar_t[dwFLen + 1];
      
                     //clear the filetxt array
                     memset(filetxt, 0, sizeof(wchar_t) * (dwFLen + 1));
                     f.Read(filetxt, dwFLen);
      
                     //use API function rather than MFC to be able
                     //to read UNICODE and ANSI at the same time
                     ::SetDlgItemTextW(this->GetSafeHwnd(), IDC_EDIT1, filetxt);
                 }
                 else
                 {
                     //ANSI files does not contain any headers, so we should
                     //reset the file pointer
                     f.SeekToBegin();
                 
                     //we are going to load the text into a character array
                     char * filetxt;
                     
                     //allocate a charaters array one for null terminator
                     filetxt = new char[dwFLen + 1];
      
                     //clear the filetxt array
                     memset(filetxt, 0, sizeof(char) * (dwFLen + 1));
                     f.Read(filetxt, dwFLen);
      
                     //use API function rather than MFC to be able
                     //to read UNICODE and ANSI at the same time
                     ::SetDlgItemTextA(this->GetSafeHwnd(), IDC_EDIT1, filetxt);
                 }
                 f.Close();
             }
      
      O 1 Reply Last reply
      0
      • M MAAK

        If I understand you right, you want to read text from file and load it into and Edit control. If it is so then this is sample code for both reading both ANSI and UNICODE text files:

               CFile f;
               if(f.open("FILENAME.TXT, CFile::modeRead))
               {
                   DWORD dwFLen = f.GetLength();
                   //this short will be used to get the file header
                   USHORT usHeader;
                   f.Read(&usHeader, sizeof(USHORT));
        
                   //UNICODE text files starts with value 0xFEFF
                   if(usHeader == 0xFEFF)
                   {
                       //we are going to load the text into a character array
                       wchar_t * filetxt;
                       
                       //allocate a charaters array one for null terminator
                       filetxt = new wchar_t[dwFLen + 1];
        
                       //clear the filetxt array
                       memset(filetxt, 0, sizeof(wchar_t) * (dwFLen + 1));
                       f.Read(filetxt, dwFLen);
        
                       //use API function rather than MFC to be able
                       //to read UNICODE and ANSI at the same time
                       ::SetDlgItemTextW(this->GetSafeHwnd(), IDC_EDIT1, filetxt);
                   }
                   else
                   {
                       //ANSI files does not contain any headers, so we should
                       //reset the file pointer
                       f.SeekToBegin();
                   
                       //we are going to load the text into a character array
                       char * filetxt;
                       
                       //allocate a charaters array one for null terminator
                       filetxt = new char[dwFLen + 1];
        
                       //clear the filetxt array
                       memset(filetxt, 0, sizeof(char) * (dwFLen + 1));
                       f.Read(filetxt, dwFLen);
        
                       //use API function rather than MFC to be able
                       //to read UNICODE and ANSI at the same time
                       ::SetDlgItemTextA(this->GetSafeHwnd(), IDC_EDIT1, filetxt);
                   }
                   f.Close();
               }
        
        O Offline
        O Offline
        orcblood
        wrote on last edited by
        #3

        Where about would I place this? Would it be right under the open dailog? Thanks, I believe that you understood me correctly. orcblood

        M 1 Reply Last reply
        0
        • O orcblood

          Where about would I place this? Would it be right under the open dailog? Thanks, I believe that you understood me correctly. orcblood

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

          I am sorry there was a little syntax error in the source code I submitted and I fixed it. The follwoing code shows how to put the file opening code after the file open dialog. if(openDlg.DoModal() == IDOK) { CFile f; if(f.open(openDlg.GetPathName(), CFile::modeRead)) . . . }

          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