text buffer conversion
-
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.
-
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.
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);
-
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);
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
-
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
Could you show me the relevent code? Where is the failiure happening?
-
Could you show me the relevent code? Where is the failiure happening?
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); }
-
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); }
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?
-
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?
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.
-
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.
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 ...
-
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 ...
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.