Home > Back-end >  I want to print all the data in the text file into the edit controller
I want to print all the data in the text file into the edit controller

Time:05-26

I can take the text file string with "fgets" and print it out one line using "SetWindowTextA". Like This code

FILE *p_file = fopen("Test.txt", "r");
if (p_file != NULL) {
  text = fgets(temp, sizeof(temp), p_file);
  m_Edit_load.SetWindowTextA(text);
  fclose(p_file)
}

But I want to print out all the lines. I've used the code at the bottom, but only the last line printed

FILE *p_file = fopen("Test.txt", "r");
if (p_file != NULL) {
  while(NULL != fgets(temp, sizeof(temp), p_file){
    m_Edit_load.SetWindowTextA(temp);
  }
  fclose(p_file);
}

How can I print out all the rows?

CodePudding user response:

Problem here is, SetWindowTextA is setting text, not appending. Hence, your window might be ending up with last line. To remove this problem, first create a dynamic array, append all characters, then call SetWindowTextA at last.

CodePudding user response:

The most straightforward way is to open the file in binary mode, load it into a buffer and put it in the control through a single SetWindowText() call.

Depending on the format of the text file, it may require some additional steps:

  • If the file is ASCII, and of the same codepage as the system it runs on, a SetWindowTextA() call is OK.
  • If the file is Unicode it can be loaded onto the control by calling SetWindowTextW() - the control must be Unicode as well.
  • If the file is UTF-8 or ASCII, but in a codepage other than that of the system, the text must be converted to Unicode using the MultiByteToWideChar() function, before loaded onto the control.
  • Another conversion that may be needed is LF to CR-LF, if the lines are joined in the control. You need to write some code for this.

CodePudding user response:

As already stated in one of the other answers, the problem is that SetWindowText will overwrite the text. Your code seems to incorrectly assume that this function will append the text instead.

If you want to set the edit control to the entire text of a file, then you will have to read the entire text of the file into a memory buffer, and pass a pointer to that memory buffer to SetWindowText.

The function fgets is used for reading a single line. Although you can solve your problem with fgets, there is no reason to limit yourself to only reading one line at a time. It would therefore be more efficient to read as much data as possible in one function call, for example by using the function fread instead of fgets.

Another issue is that you are opening your text file in text mode, which means that the \r\n line endings will get translated to \n. However, this is not what you want, because when using SetWindowText on a multiline edit control, the line endings must be \r\n, not \n. Therefore, you should change the line

FILE *p_file = fopen("Test.txt", "r");

to

FILE *p_file = fopen("Test.txt", "rb");

in order to open the file in binary mode.

The whole code should look like this:

FILE *fp = fopen( "Test.txt", "rb" );
if ( fp != NULL )
{
    char buffer[4096];
    size_t bytes_read;

    bytes_read = fread( buffer, 1, (sizeof buffer) - 1, fp );
    buffer[bytes_read] = '\0';

    m_Edit_load.SetWindowTextA( buffer );

    fclose(p_file);
}

If it is possible that 4096 bytes is not sufficient to contain the entire file, then you could increase the size of the buffer. However, you should not increase it too much, because otherwise, there is a danger of a stack overflow. Instead of allocating the memory buffer on the stack, you could also allocate it on the heap, by using malloc instead. Another alternative would be to use a static buffer, which also does not get allocated on the stack.

  • Related