Download MetaTrader 5

\n not recognized when reading from text file -> string array -> MessageBox()

To add comments, please log in or register
xcuco3x
19
xcuco3x  

I'm reading from a text file, inputting the data into a string array, and outputting one of the lines from the string array into a message box.

I believe it's not due to using a string array because I created one separately, tried it out, and the new line character was read perfectly. Therefore, I believe it is due to how the text is read from the text file using FileReadString().

Expected Result: MessageBox() reads the new line character and applies it properly

Testing Line One
Testing Line Two

Actual Result: MessageBox() ignores the new line character and outputs it directly

Testing Line One\nTesting Line Two

void displayMessage()
{
   int maxNum = ArraySize(mArray);
   if (maxNum > 0)
   {   
      int rng = 0 + maxNum*MathRand()/32768;
      MessageBox(mArray[rng],"My Messages");
      mmCooldown = TimeCurrent()+mTimer;
   }   
}
void openFile()
{
   handle = FileOpen("my_messages.txt",FILE_READ|FILE_SHARE_READ|FILE_TXT);
   
   while (handle != 1)
   {
      errorResult = MessageBox("Error: could not open file."+
         "\r\n\r\nMake sure the my_messages.txt file is located in the MQL4\\Files folder and press 'Ok' to continue"+
         "\r\n\r\nPress 'Cancel' to disable My Messages",
         "File Not Found",MB_OKCANCEL);
      if (errorResult == 1)
      {
         handle = FileOpen("my_messages.txt",FILE_READ|FILE_SHARE_READ|FILE_TXT);         
      }
      else
      {
         enableMessages = false;
         return;
      }
   }   
   int x = 0;      
   while (!FileIsEnding(handle))
   {
      ArrayResize(mArray,x+1);
      mArray[x] = FileReadString(handle);
      x++;         
   }
   FileClose(handle);
}
whroeder1
16808
whroeder1  

Expected Result: MessageBox() reads the new line character and applies it properly

Testing Line One
Testing Line Two
Which is exactly what you get
void OnStart()
{  
MessageBox("Error: could not open file."
         "\n\nMake sure the my_messages.txt file is located in the MQL4\\Files folder and press 'Ok' to continue"
         "\n\nPress 'Cancel' to disable My Messages",
         "File Not Found", MB_OKCANCEL);
         }


Drop the "\r" unnecessary. I dropped the "+" It auto-combines adjacent literal strings.
xcuco3x
19
xcuco3x  
whroeder1:

Expected Result: MessageBox() reads the new line character and applies it properly

Which is exactly what you get

Drop the "\r" unnecessary. I dropped the "+" It auto-combines adjacent literal strings.

I don't think you understand.

The part you quoted, I'm just displaying the code I used for reading the data from the text file.

The focus is on the displayMessage function, where a random element from the string array created from openFile() is outputted into the MessageBox() function.

whroeder1
16808
whroeder1  

I definitely don't. Why are you talking about MessageBox when you have a problem reading a text file.

There is nothing wrong with your displayMessage function as is. You have lines of text in the array, you message box one of them.

What you want, If I understand you, you want to read text from the file that is multiline. Why didn't you say that? You totally confused everything with the rest.

You can't read newlines from a text file. They terminate the line.

If you want multi-lines read, your file must contain a marker character(s) (not used in regular text e.g. "¶") and then substitute them (StringReplace) with \n in the MessageBox call.

xcuco3x
19
xcuco3x  
whroeder1:

I definitely don't. Why are you talking about MessageBox when you have a problem reading a text file.

You can't read newlines from a text file. They terminate the line.

If you want multi-lines read, your file must contain a marker character(s) (not used in regular text e.g. "@ ` ¦ © ¶") and then substitute them (StringReplace) with \n in the MessageBox call.

1. Read from file

2. Data from file to string array

3. Data from string array to MessageBox()

If I create a separate string array, such as,

sArray[] = { "Testing Line One\nTesting Line Two" };

and then

MessageBox(sArray[0],"My Messages");

I get the expected result.

But if I read the same string from a text file, put that string into a string array, and then read from that element in the string array into a MessageBox(), the \n special character is read explicitly rather than parsed as a new line.

It seems like the last sentence of your post is the workaround, as reading a special character from a string that was inputted by FileReadString() isn't parsed properly. I'll try that out.
whroeder1
16808
whroeder1  

xcuco3x:But if I read the same string from a text file,

What part of
You can't read newlines from a text file. They terminate the line.
was unclear? You can't read the new lines.
xcuco3x
19
xcuco3x  
whroeder1:

What part ofwas unclear? You can't read the new lines.
I'm not trying to read the new line from the text file. I need it to parse from the MessageBox from the string array. What part of that was unclear?
honest_knave
Moderator
2325
honest_knave  

I'm a little unclear too.

Contents of Messages.txt:

This is line 1\nThis is line 2

This script:

   int handle = FileOpen("Messages.txt",FILE_READ|FILE_TXT);
   if(handle==INVALID_HANDLE) return;
   while(!FileIsEnding(handle)) Print(FileReadString(handle));
   FileClose(handle);

Returns this:

2017.04.06 16:24:04.399	Test AUDCHF,M1: This is line 1\nThis is line 2

However, if you change Messages.txt to this:

This is line 1
This is line 2

The result is:

2017.04.06 16:24:55.826	Test AUDCHF,M1: This is line 2
2017.04.06 16:24:55.826	Test AUDCHF,M1: This is line 1

If I understand correctly, you'd like to know why this isn't being recognised?

This is line 1\nThis is line 2
whroeder1
16808
whroeder1  
xcuco3x: I'm not trying to read the new line from the text file.
That is exactly what you said you are trying to do.
xcuco3x:

If I create a separate string array, such as,

sArray[] = { "Testing Line One\nTesting Line Two" };

and then

MessageBox(sArray[0],"My Messages");

I get the expected result.

But if I read the same string from a text file, put that string into a string array,

You can not read that string from a text file. You can not create a text file with an embedded new line.

To add comments, please log in or register