Any questions from newcomers on MQL4 and MQL5, help and discussion on algorithms and codes - page 481

 
STARIJ:

When opening the file for writing you specified TXT or CSV. This is a text file. Read it as a string, select StringSubstr and convert it to what you want

void Write_File()
{
    int handle;
    static double BlueL,RedL;
    string B_level= DoubleToStr(BlueLine, Digits);
    string R_level= DoubleToStr(RedLine, Digits);
    string f_name = " (" + Symbol() + ")\\" + TimeToStr( LocalTime(),TIME_DATE ) + ".txt";
     
    
      if(BlueLine!=BlueL||RedLine!=RedL)
       {
       Sleep(10000);//подождем 10 сек, пока оператор меняет уровни
       
      // Открытие или создание файла и перемещение указателя в конец
      handle= FileOpen(f_name,FILE_CSV|FILE_READ|FILE_WRITE," ");
      if(handle == -1) {  Alert("Ошибка при открытии файла ",handle);  return; }
      FileSeek(handle,0,SEEK_END);

      // Если новый файл - записать имя файла и строку заголовков колонок
      if(FileSize(handle)==0)
      {
         FileWrite(handle,f_name);
         FileWrite(handle,"Изменение Уровней   BlueLine   RedLine");
      }
      // Сбор информации и запись в файл
      FileWrite(handle,TimeToStr(TimeCurrent()), "  ",B_level, "  ",R_level);
      FileClose(handle);
      BlueL=BlueLine;RedL=RedLine;
      }
      return;
   }

That's the way to write! TCT

I'm sorry I don't understand how to get exactlyB_level andR_level out now.I don't need date and time! I do want to write them down though)

 
Rewerpool:

That's the way to write! TCT

I'm sorry I don't understand how to get exactlyB_level andR_level out now.I don't need date and time! But I do want to write it down)

Read as string, highlight StringSubstr and convert to what you need. Read as string, allocate StringSubstr and convert to whatever you need

 
STARIJ:

Read as a string, allocate a StringSubstr and convert to whatever you need. Read as a string, select StringSubstr and convert to whatever

Ah ah ah ah ah ah, got it !!! That's it !!! Thanks@STARIJ! I'll give it a try)

 

@STARIJ not working, can't figure out where I'm wrong yet! Help!

double BlueLine,RedLine;

void Read_File()
{
    int handle;
    string B_level;
    string R_level;
    static double BlueL=StrToDouble(B_level);
    static double RedL=StrToDouble(R_level);
    
    string f_name = " (" + Symbol() + ")\\" + TimeToStr( LocalTime(),TIME_DATE ) + ".txt";
     
     if(BlueL!=BlueLine||RedL!=RedLine)
     {
      // Открытие или создание файла и перемещение указателя в конец
      handle= FileOpen(f_name,FILE_CSV|FILE_READ|FILE_WRITE," ");
      if(handle == -1) {  Alert("Ошибка при открытии файла ",handle);  return; }
      FileSeek(handle,0,SEEK_END);
      
      // Чтение информации из файла
      FileReadString(handle,B_level= StringSubstr(f_name,16,7),R_level= StringSubstr(f_name,23,7));
      FileClose(handle);
      
      BlueLine=BlueL;RedLine=RedL;
      }
      return;
   }
1 Mistake here, but what have I done wrong?
 

Why doesn't he like that?

2

 
Rewerpool:

Why doesn't it like this?


Look at what parameters you pass to the function, and how many, and what it returns.

click on f1, put your cursor on this function beforehand

 
Rewerpool:

@STARIJ not working, can't figure out where I'm wrong yet! Help!

Mistake here, but what have I done wrong?
First let's read the last string
   int handla = FileOpen("1111.txt",FILE_TXT|FILE_READ," ");   // TXT !!!!!!!
   string s;
   while(!FileIsEnding(handla))  s=FileReadString(handla,100);
   Alert(s);
   FileClose(handla);

2018.02.26 12:49 1.24368 1.22581 Now ... Highlight StringSubstr and convert to what you need. Everything has to be done step by step. Haste is an enemy of a programmer.

   double Синяя=StrToDouble(StringSubstr(s,20,7));
   double Красн=StrToDouble(StringSubstr(s,31,7));
   Alert(DoubleToStr(Синяя,5), "   ", DoubleToStr(Красн,5));

Read as string, allocate StringSubstr and convert to string. Read as string, allocate StringSubstr and convert to a string

 
STARIJ:
First let's read the last string

2018.02.26 12:49 1.24368 1.22581 Now ... allocate StringSubstr and convert to what you need. You have to do everything step by step. Haste is an enemy of a programmer.

Read as string, allocate StringSubstr and convert to string. Read as string, allocate StringSubstr and convert to a string

STARIJ: Thanks for the breakdown!

Reworked it! Now the numbers don't add up for some reason!

The numbers in the file are like this:

2018.02.26 17:44    1.24938    1.22771

And the same Alert gives these:

Where did he get the zeros after 1.2? I've tried normalising. Nope!

1

The code is written like this:

void Read_File()
{
    int handle;
    string str;
    static double BlueL;
    static double RedL;
    string f_name = " (" + Symbol() + ")\\" + TimeToStr( LocalTime(),TIME_DATE ) + ".txt";
     
     if(BlueL!=BlueLine||RedL!=RedLine)
     {
      // Открытие или создание файла и перемещение указателя в конец
      handle= FileOpen(f_name,FILE_READ|FILE_TXT," ");
      if(handle == -1) {  Alert("Ошибка при открытии файла ",handle);  return; }
      
       while(!FileIsEnding(handle)) //Читаем последнюю строку
       str=FileReadString(handle,60);//Чтение строки из файла
       FileClose(handle);
       BlueL=StrToDouble(StringSubstr(str,16,7));
       RedL=StrToDouble(StringSubstr(str,23,7));
       Alert(DoubleToStr(BlueL,5), "   ", DoubleToStr(RedL,5));
       ObjectSet("BlueLine",OBJPROP_PRICE1,BlueL);
       ObjectSet("RedLine",OBJPROP_PRICE1,RedL);
      }
      return;
   }
 
Rewerpool:

STARIJ : Thanks for the breakdown!
Reworked it! Now the numbers don't add up for some reason!
The numbers in the file are:
But the same Alert gives these:
Where did it get the zeros after 1.2 ? I've tried normalising. Nope!
I wrote the code like this:

I had StringSubstr(str,20,7) and you write StringSubstr(str,16,7). Subtract the last 4 digits from 1.24938 and get 1.2. I told you - do everything sequentially. I had Alert(s); - print the string you've read to make sure it's correct, while you are in a hurry to write ObjectSet(. Programming is all about patience, small steps forward - while you have shouting on the forum. While you are waiting for an answer - how many small confident steps you can take!!!
 
Rewerpool:

STARIJ: Thanks for the breakdown!

Reworked it! Now the numbers don't add up for some reason!

The numbers in the file are like this:

And the same Alert gives these:

Where did he get the zeros after 1.2? I've tried normalising. Nope!

I wrote the code like this:

Try this.


void Read_File()
{
    int handle;
    string str;
    static double BlueL;
    static double RedL;
    string f_name = " (" + Symbol() + ")\\" + TimeToStr( LocalTime(),TIME_DATE ) + ".txt";
     
     if(BlueL!=BlueLine||RedL!=RedLine)
     {
      // Открытие или создание файла и перемещение указателя в конец
      handle= FileOpen(f_name,FILE_READ|FILE_TXT," ");
      if(handle == -1) {  Alert("Ошибка при открытии файла ",handle);  return; }
      
       while(!FileIsEnding(handle)) //Читаем последнюю строку
       str=FileReadString(handle,60);//Чтение строки из файла
       FileClose(handle);
       BlueL=StrToDouble(StringSubstr(str,20,7));   
       RedL=StrToDouble(StringSubstr(str,31,7));    
       Alert(DoubleToStr(BlueL,5), "   ", DoubleToStr(RedL,5));
       ObjectSet("BlueLine",OBJPROP_PRICE1,BlueL);
       ObjectSet("RedLine",OBJPROP_PRICE1,RedL);
      }
      return;
   }