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. text buffer conversion

text buffer conversion

Scheduled Pinned Locked Moved C / C++ / MFC
9 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.
  • F Offline
    F Offline
    FredrickNorge
    wrote on last edited by
    #1

    hi, i am loading a text file into this buffer , FILE *buffer = fopen("text.txt", "r"); i need to print it out again in a LPCTSTR pszText, i need a simple conversion for that thanks.

    W 1 Reply Last reply
    0
    • F FredrickNorge

      hi, i am loading a text file into this buffer , FILE *buffer = fopen("text.txt", "r"); i need to print it out again in a LPCTSTR pszText, i need a simple conversion for that thanks.

      W Offline
      W Offline
      Waldermort
      wrote on last edited by
      #2

      You will need to allocate a buffer and call fread()

      FILE \*infile;
      if ((infile = \_tfopen(sFilename,\_T("rb") )) == NULL) {
      	return FALSE;
      }
      
      // obtain file size.
      fseek(infile , 0 , SEEK\_END);
      long lSize = ftell(infile);
      rewind(infile);
      
      //read in the contents of the input file
      char\* pszIn = new char\[lSize\];
      fread(pszIn,1,lSize,infile);
      fclose(infile);
      
      F 1 Reply Last reply
      0
      • W Waldermort

        You will need to allocate a buffer and call fread()

        FILE \*infile;
        if ((infile = \_tfopen(sFilename,\_T("rb") )) == NULL) {
        	return FALSE;
        }
        
        // obtain file size.
        fseek(infile , 0 , SEEK\_END);
        long lSize = ftell(infile);
        rewind(infile);
        
        //read in the contents of the input file
        char\* pszIn = new char\[lSize\];
        fread(pszIn,1,lSize,infile);
        fclose(infile);
        
        F Offline
        F Offline
        FredrickNorge
        wrote on last edited by
        #3

        Ok that worked but it triggers and character test assert (<= 256) and if i ignore it i get the blablaí0 the text file contained blabla

        W 1 Reply Last reply
        0
        • F FredrickNorge

          Ok that worked but it triggers and character test assert (<= 256) and if i ignore it i get the blablaí0 the text file contained blabla

          W Offline
          W Offline
          Waldermort
          wrote on last edited by
          #4

          Could you show me the relevent code? Where is the failiure happening?

          F 1 Reply Last reply
          0
          • W Waldermort

            Could you show me the relevent code? Where is the failiure happening?

            F Offline
            F Offline
            FredrickNorge
            wrote on last edited by
            #5

            text is the final conversion, taken from bellow. m_pTextBuffer->InsertText(this, ptCursorPos.y, ptCursorPos.x, text, y, x, CE_ACTION_PASTE); void OnScriptMission() { if (m_pTextBuffer == NULL) return; m_pTextBuffer->BeginUndoGroup(); FILE *buffer; if ((buffer = _tfopen("text.txt",_T("rb") )) == NULL) { return; } // obtain file size. fseek(buffer , 0 , SEEK_END); long lSize = ftell(buffer); rewind(buffer); //read in the contents of the input file char *text = new char[lSize]; fread(text,1,lSize,buffer); CPoint ptCursorPos = GetCursorPos(); ASSERT_VALIDTEXTPOS(ptCursorPos); int x, y; m_pTextBuffer->InsertText(this, ptCursorPos.y, ptCursorPos.x, text, y, x, CE_ACTION_PASTE); ptCursorPos.x = x; ptCursorPos.y = y; ASSERT_VALIDTEXTPOS(ptCursorPos); SetAnchor(ptCursorPos); SetSelection(ptCursorPos, ptCursorPos); SetCursorPos(ptCursorPos); EnsureVisible(ptCursorPos); m_pTextBuffer->FlushUndoGroup(this); fclose(buffer); }

            W 1 Reply Last reply
            0
            • F FredrickNorge

              text is the final conversion, taken from bellow. m_pTextBuffer->InsertText(this, ptCursorPos.y, ptCursorPos.x, text, y, x, CE_ACTION_PASTE); void OnScriptMission() { if (m_pTextBuffer == NULL) return; m_pTextBuffer->BeginUndoGroup(); FILE *buffer; if ((buffer = _tfopen("text.txt",_T("rb") )) == NULL) { return; } // obtain file size. fseek(buffer , 0 , SEEK_END); long lSize = ftell(buffer); rewind(buffer); //read in the contents of the input file char *text = new char[lSize]; fread(text,1,lSize,buffer); CPoint ptCursorPos = GetCursorPos(); ASSERT_VALIDTEXTPOS(ptCursorPos); int x, y; m_pTextBuffer->InsertText(this, ptCursorPos.y, ptCursorPos.x, text, y, x, CE_ACTION_PASTE); ptCursorPos.x = x; ptCursorPos.y = y; ASSERT_VALIDTEXTPOS(ptCursorPos); SetAnchor(ptCursorPos); SetSelection(ptCursorPos, ptCursorPos); SetCursorPos(ptCursorPos); EnsureVisible(ptCursorPos); m_pTextBuffer->FlushUndoGroup(this); fclose(buffer); }

              W Offline
              W Offline
              Waldermort
              wrote on last edited by
              #6

              It;s hard to tell where it's going wrong from the code you posted. What is the insertText() function? Also, since you're already using MFC, why not use the CFile class to read the file?

              F 1 Reply Last reply
              0
              • W Waldermort

                It;s hard to tell where it's going wrong from the code you posted. What is the insertText() function? Also, since you're already using MFC, why not use the CFile class to read the file?

                F Offline
                F Offline
                FredrickNorge
                wrote on last edited by
                #7

                hardcoded char array works though :( insertText is to keep track on undo \redo etc, but ive tested hardcoded data , and they work so i doubt there is anything wrong with it. i can give CFile a try, thanks for your help.

                F 1 Reply Last reply
                0
                • F FredrickNorge

                  hardcoded char array works though :( insertText is to keep track on undo \redo etc, but ive tested hardcoded data , and they work so i doubt there is anything wrong with it. i can give CFile a try, thanks for your help.

                  F Offline
                  F Offline
                  FredrickNorge
                  wrote on last edited by
                  #8

                  this fixed the problem //read in the contents of the input file char *text = new char[lSize+1]; fread(text,1,lSize,buffer); text[lSize] = 0; // // a newline ...

                  W 1 Reply Last reply
                  0
                  • F FredrickNorge

                    this fixed the problem //read in the contents of the input file char *text = new char[lSize+1]; fread(text,1,lSize,buffer); text[lSize] = 0; // // a newline ...

                    W Offline
                    W Offline
                    Waldermort
                    wrote on last edited by
                    #9

                    Ah yes, my mistake, you are dealing with strings so you would need to allow for the null terminator. That snippet comes from a binary file reader where the extra character would corrupt my data stream.

                    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