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

Dumb CFile question..

Scheduled Pinned Locked Moved C / C++ / MFC
questionhelp
8 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.
  • R Offline
    R Offline
    RobJones
    wrote on last edited by
    #1

    Hello, This is a dumb question but I'm using the following to stream RTF from a .rtf file to a CRichEditCtrl.. The problem is that it's only reading in the 4096, I understand it's because my cBuf is only 4096.. Is there a way to read the whole file in with out having to declare the size of the cBuf char?? Heres my code..

    // READ THE .RTF FILE IN.
    CFile fileRead;
    if(fileRead.Open(strFile,CFile::modeRead))
    {
    char cBuf[4096];
    UINT uBytesRead;

    while(uBytesRead = fileRead.Read(cBuf, sizeof(cBuf)-1))
    {
    	cBuf\[uBytesRead\] = NULL;
    	SetRTF(CString(cBuf));
    }
    					
    fileRead.Close();
    

    }

    Whoever said nothing's impossible never tried slamming a revolving door!

    P B 2 Replies Last reply
    0
    • R RobJones

      Hello, This is a dumb question but I'm using the following to stream RTF from a .rtf file to a CRichEditCtrl.. The problem is that it's only reading in the 4096, I understand it's because my cBuf is only 4096.. Is there a way to read the whole file in with out having to declare the size of the cBuf char?? Heres my code..

      // READ THE .RTF FILE IN.
      CFile fileRead;
      if(fileRead.Open(strFile,CFile::modeRead))
      {
      char cBuf[4096];
      UINT uBytesRead;

      while(uBytesRead = fileRead.Read(cBuf, sizeof(cBuf)-1))
      {
      	cBuf\[uBytesRead\] = NULL;
      	SetRTF(CString(cBuf));
      }
      					
      fileRead.Close();
      

      }

      Whoever said nothing's impossible never tried slamming a revolving door!

      P Offline
      P Offline
      Pavel Klocek
      wrote on last edited by
      #2

      You can use CRichEditCtrl::ReplaceSel to append text in to the control. Pavel Sonork 100.15206

      R 1 Reply Last reply
      0
      • P Pavel Klocek

        You can use CRichEditCtrl::ReplaceSel to append text in to the control. Pavel Sonork 100.15206

        R Offline
        R Offline
        RobJones
        wrote on last edited by
        #3

        It's not stopping at 4096.. but it is stopping before the end.. I wonder if the CRichEditCtrl has a text/size limit (I didn't declare one in my dialogs Init). I changed the 4096 to 1024 and it still reads in the same amount.. I re-wrote the code like this..

        // READ THE .RTF FILE IN.
        CFile fileRead;
        if(fileRead.Open(strFile,CFile::modeRead))
        {
        char cBuf[1024];
        UINT uBytesRead;
        CString strBuff;

        while(uBytesRead = fileRead.Read(cBuf, sizeof(cBuf)-1))
        {
        	cBuf\[uBytesRead\] = NULL;
        	strBuff += CString(cBuf);
        }
        
        SetRTF(strBuff);			
        fileRead.Close();
        

        }

        Whoever said nothing's impossible never tried slamming a revolving door!

        C P 2 Replies Last reply
        0
        • R RobJones

          It's not stopping at 4096.. but it is stopping before the end.. I wonder if the CRichEditCtrl has a text/size limit (I didn't declare one in my dialogs Init). I changed the 4096 to 1024 and it still reads in the same amount.. I re-wrote the code like this..

          // READ THE .RTF FILE IN.
          CFile fileRead;
          if(fileRead.Open(strFile,CFile::modeRead))
          {
          char cBuf[1024];
          UINT uBytesRead;
          CString strBuff;

          while(uBytesRead = fileRead.Read(cBuf, sizeof(cBuf)-1))
          {
          	cBuf\[uBytesRead\] = NULL;
          	strBuff += CString(cBuf);
          }
          
          SetRTF(strBuff);			
          fileRead.Close();
          

          }

          Whoever said nothing's impossible never tried slamming a revolving door!

          C Offline
          C Offline
          Chris Meech
          wrote on last edited by
          #4

          I think it is because your while ( condition ) is testing the result of an assignment operation instead of testing for an end of file condition. Try something like this

          uBytesRead = fileRead.Read(cBuf, sizeof(cBuf)-1);
          while ( uBytesRead == 1024 )
          {
          strBuff += CString(cBuf);
          uBytesRead = fileRead.Read(cBuf, sizeof(cBuf)-1));
          }

          strBuff += CString(cBuf);
          SetRTF(strBuff);
          fileRead.Close();

          Chris Meech "what makes CP different is the people and sense of community, things people will only discover if they join up and join in." Christian Graus Nov 14, 2002. "AAAAAAAAAHHHHHH!!!!! Those leaks are driving me crazy! How does one finds a memory leak in a garbage collected environment ??! Daniel Turini Nov. 2, 2002.

          R 1 Reply Last reply
          0
          • R RobJones

            It's not stopping at 4096.. but it is stopping before the end.. I wonder if the CRichEditCtrl has a text/size limit (I didn't declare one in my dialogs Init). I changed the 4096 to 1024 and it still reads in the same amount.. I re-wrote the code like this..

            // READ THE .RTF FILE IN.
            CFile fileRead;
            if(fileRead.Open(strFile,CFile::modeRead))
            {
            char cBuf[1024];
            UINT uBytesRead;
            CString strBuff;

            while(uBytesRead = fileRead.Read(cBuf, sizeof(cBuf)-1))
            {
            	cBuf\[uBytesRead\] = NULL;
            	strBuff += CString(cBuf);
            }
            
            SetRTF(strBuff);			
            fileRead.Close();
            

            }

            Whoever said nothing's impossible never tried slamming a revolving door!

            P Offline
            P Offline
            Pavel Klocek
            wrote on last edited by
            #5

            I think, You need to read the whole file and then set it, because of the RTF format which is structured, and can't be added into RichEdit in chunks split at random. Pavel Sonork 100.15206

            1 Reply Last reply
            0
            • C Chris Meech

              I think it is because your while ( condition ) is testing the result of an assignment operation instead of testing for an end of file condition. Try something like this

              uBytesRead = fileRead.Read(cBuf, sizeof(cBuf)-1);
              while ( uBytesRead == 1024 )
              {
              strBuff += CString(cBuf);
              uBytesRead = fileRead.Read(cBuf, sizeof(cBuf)-1));
              }

              strBuff += CString(cBuf);
              SetRTF(strBuff);
              fileRead.Close();

              Chris Meech "what makes CP different is the people and sense of community, things people will only discover if they join up and join in." Christian Graus Nov 14, 2002. "AAAAAAAAAHHHHHH!!!!! Those leaks are driving me crazy! How does one finds a memory leak in a garbage collected environment ??! Daniel Turini Nov. 2, 2002.

              R Offline
              R Offline
              RobJones
              wrote on last edited by
              #6

              It still stops at the same spot.. Maybe theres something wrong with SetRTF.. weird, like the person said after your post, if im not reading the whole file the SetRTF wouldn't work because I wouldn't have a complete RTF string, so I'm guessing that this "read part" is probably working it's just not displaying the whole file... weird. Any way thanks everyone for helping me, im going to look deeper into my SetRTF function. And im going to look at the string before SetRTF gets called. Thanks again, Rob Whoever said nothing's impossible never tried slamming a revolving door!

              1 Reply Last reply
              0
              • R RobJones

                Hello, This is a dumb question but I'm using the following to stream RTF from a .rtf file to a CRichEditCtrl.. The problem is that it's only reading in the 4096, I understand it's because my cBuf is only 4096.. Is there a way to read the whole file in with out having to declare the size of the cBuf char?? Heres my code..

                // READ THE .RTF FILE IN.
                CFile fileRead;
                if(fileRead.Open(strFile,CFile::modeRead))
                {
                char cBuf[4096];
                UINT uBytesRead;

                while(uBytesRead = fileRead.Read(cBuf, sizeof(cBuf)-1))
                {
                	cBuf\[uBytesRead\] = NULL;
                	SetRTF(CString(cBuf));
                }
                					
                fileRead.Close();
                

                }

                Whoever said nothing's impossible never tried slamming a revolving door!

                B Offline
                B Offline
                Brigsoft
                wrote on last edited by
                #7

                Question: What is SetRTF()? ================================ Useful links

                R 1 Reply Last reply
                0
                • B Brigsoft

                  Question: What is SetRTF()? ================================ Useful links

                  R Offline
                  R Offline
                  RobJones
                  wrote on last edited by
                  #8

                  I use SetRTF to stream an RTF string into a RichEdit control or view.. It's declared like this..

                  // in the .h
                  void SetRTF(CString sRTF);
                  static DWORD CALLBACK CBStreamIn(DWORD dwCookie, LPBYTE pbBuff, LONG cb, LONG *pcb);

                  // in the .cpp
                  void CMyView::SetRTF(CString sRTF)
                  {
                  // Read the text in
                  EDITSTREAM es;
                  es.dwError = 0;
                  es.pfnCallback = CBStreamIn;
                  es.dwCookie = (DWORD) &sRTF;
                  GetRichEditCtrl().StreamIn(SF_RTF | SFF_SELECTION, es);
                  }

                  DWORD CALLBACK CMyView::CBStreamIn(DWORD dwCookie, LPBYTE pbBuff, LONG cb, LONG *pcb)
                  {
                  CString *pstr = (CString *)dwCookie;
                  USES_CONVERSION;
                  if( pstr->GetLength() < cb )
                  {
                  *pcb = pstr->GetLength();
                  memcpy(pbBuff, T2CA((LPCTSTR)*pstr), *pcb );
                  pstr->Empty();
                  }
                  else
                  {
                  *pcb = cb;
                  memcpy(pbBuff, T2CA((LPCTSTR)*pstr), *pcb );
                  *pstr = pstr->Right( pstr->GetLength() - cb );
                  }

                  return 0;
                  

                  }

                  Rob Whoever said nothing's impossible never tried slamming a revolving door!

                  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