Unlimited or arbitrary string lengths?
-
Hi, I'm writing a small program with a basic text editor incorporated. It is basically a dialog box with a rich edit box and the capability of saving what is written in the rich edit box to a .txt file. Here is the function I am using to save the text (I'm using _no_ MFC, by the way): [code] void SaveRichEditText(HWND hWnd, int nIDDlgItem, char* szSaveFile) { char szText[100000]=""; GetDlgItemText(hWnd,nIDDlgItem,szText,100000); HANDLE hFile = CreateFile(szSaveFile,GENERIC_WRITE,0,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL); DWORD dwW; WriteFile(hFile,szText,strlen(szText),&dwW,NULL); CloseHandle(hFile); } [/code] This works fine, and the file is saved with the correct text. However, as you can easily see from the code, this has a severe restriction: the text that can be saved is limited to 100,000 characters (or any other arbitrary limit I choose for char szText[]). This is no doubt a very stupid and basic question, but I can't find the answer in my Sams or "C for Dummies" or other books, so please forgive the lack of knowledge of a relative novice: How do I make it so that the text length is not limited by the char? Is there a way to get the length of the text in the Rich Edit box and then create a string on the fly that is the length of this? I am sure I have seen somewhere a method using mem... commands, but can't find the answer. Once this is solved, I would then need to create a load command that reads the text from file using ReadFile(), and I will be faced with a similar problem - namely getting the length of the string from the .txt file and creating that string to insert into my rich edit box. If anyone can tell me how to go about solving these two problems, I would be really grateful - apologies again if this is a really basic question. Many thanks, KB
-
Hi, I'm writing a small program with a basic text editor incorporated. It is basically a dialog box with a rich edit box and the capability of saving what is written in the rich edit box to a .txt file. Here is the function I am using to save the text (I'm using _no_ MFC, by the way): [code] void SaveRichEditText(HWND hWnd, int nIDDlgItem, char* szSaveFile) { char szText[100000]=""; GetDlgItemText(hWnd,nIDDlgItem,szText,100000); HANDLE hFile = CreateFile(szSaveFile,GENERIC_WRITE,0,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL); DWORD dwW; WriteFile(hFile,szText,strlen(szText),&dwW,NULL); CloseHandle(hFile); } [/code] This works fine, and the file is saved with the correct text. However, as you can easily see from the code, this has a severe restriction: the text that can be saved is limited to 100,000 characters (or any other arbitrary limit I choose for char szText[]). This is no doubt a very stupid and basic question, but I can't find the answer in my Sams or "C for Dummies" or other books, so please forgive the lack of knowledge of a relative novice: How do I make it so that the text length is not limited by the char? Is there a way to get the length of the text in the Rich Edit box and then create a string on the fly that is the length of this? I am sure I have seen somewhere a method using mem... commands, but can't find the answer. Once this is solved, I would then need to create a load command that reads the text from file using ReadFile(), and I will be faced with a similar problem - namely getting the length of the string from the .txt file and creating that string to insert into my rich edit box. If anyone can tell me how to go about solving these two problems, I would be really grateful - apologies again if this is a really basic question. Many thanks, KB
try this: void SaveRichEditText(HWND hWnd, int nIDDlgItem, char* szSaveFile) { // Counters uint nCount = 0; uint nNewCount = 0; BOOL bRet = FALSE; char* szText = new char[100000]; // Get the initial count returned nNewCount = GetDlgItemText(hWnd,nIDDlgItem,szText,100000); // Keep getting the count until it returns 0 while( nNewCount > 0 ) { nCount += nNewCount; nNewCount = GetDlgItemText(hWnd,nIDDlgItem,szText,100000); } // If there is nothing to save return or insert code to create the file and return. if( 0 == nCount ) { return; } // Delete the array. delete[] szText; // Create the array the necessary size (nCount + 1(NULL) ) szText = new char[++nCount]; // Get all of the data. GetDlgItemText(hWnd,nIDDlgItem,szText,nCount); // Create the file HANDLE hFile = CreateFile(szSaveFile,GENERIC_WRITE,0,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL); DWORD dwW; // Save the data. bRet = WriteFile(hFile,szText,nCount,&dwW,NULL); // Close the file CloseHandle(hFile); // Check for write error if( TRUE != bRet ) { // Handle Error } } Its probably not the cleanest way of doing things, but it is never the less a way. Hope it helps.
-
Hi, I'm writing a small program with a basic text editor incorporated. It is basically a dialog box with a rich edit box and the capability of saving what is written in the rich edit box to a .txt file. Here is the function I am using to save the text (I'm using _no_ MFC, by the way): [code] void SaveRichEditText(HWND hWnd, int nIDDlgItem, char* szSaveFile) { char szText[100000]=""; GetDlgItemText(hWnd,nIDDlgItem,szText,100000); HANDLE hFile = CreateFile(szSaveFile,GENERIC_WRITE,0,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL); DWORD dwW; WriteFile(hFile,szText,strlen(szText),&dwW,NULL); CloseHandle(hFile); } [/code] This works fine, and the file is saved with the correct text. However, as you can easily see from the code, this has a severe restriction: the text that can be saved is limited to 100,000 characters (or any other arbitrary limit I choose for char szText[]). This is no doubt a very stupid and basic question, but I can't find the answer in my Sams or "C for Dummies" or other books, so please forgive the lack of knowledge of a relative novice: How do I make it so that the text length is not limited by the char? Is there a way to get the length of the text in the Rich Edit box and then create a string on the fly that is the length of this? I am sure I have seen somewhere a method using mem... commands, but can't find the answer. Once this is solved, I would then need to create a load command that reads the text from file using ReadFile(), and I will be faced with a similar problem - namely getting the length of the string from the .txt file and creating that string to insert into my rich edit box. If anyone can tell me how to go about solving these two problems, I would be really grateful - apologies again if this is a really basic question. Many thanks, KB
-
I'll suggest you to use RichEdit messages, EM_GETTEXTLENGTHEX to get length of text and create bufer. and EM_GETTEXTEX to get text and fill buffer
Great, thanks for the info, and thanks for the replies to both of you. In the end I went the EM_GETTEXTLENGTHEX route and used that to create the buffer, though I had problems before realising that I needed to load RichEd20.dll instead of RichEd32.dll, and then, because I was using a dialog-based app, I had to manually change the class name in the .rc file to "RICHEDIT20A" instead of "RICHEDIT" (just in case anyone else encounters this problem). Thanks again! KB