Saving a parameter of a function in a .txt file in rows

To add comments, please log in or register
Batuhan Ortay
172
Batuhan Ortay  
void Log_On_Chart(string labelText, color labelColor)
  {
   int file_handle = FileOpen("abc.txt", FILE_READ|FILE_WRITE|FILE_TXT);
   if(file_handle != INVALID_HANDLE)
     {
      FileWrite(file_handle, labelText + "\n");
      FileClose(file_handle);
     }
   else
      PrintFormat("Failed to open %s file, Error code = %d", GetLastError());
   
   ....
   ....
  }


I want to save the labelText parameter of this function into a .txt file in rows, but "\n" does not work here, the first row is always replaced by the values coming with the next calls of the function.

How can I write the values of this parameter in a .txt file in rows?

Thanks.

lippmaje
1037
lippmaje  
Batuhan:

..., the first row is always replaced by the values coming with the next calls of the function.

By calling FileOpen you reset the file cursor to the beginning. It's like opening a text in Word and placing the cursor to the start. Then you write a line and close it. The previous line is overwritten.

You need to open and close the file outside the log function like so:

int LogHandle;

int OnInit() {
   LogHandle = FileOpen("abc.txt", FILE_READ|FILE_WRITE|FILE_TXT);
   if(LogHandle==INVALID_HANDLE) {
      PrintFormat("Failed to open %s file, Error code = %d", "abc.txt", GetLastError());
      return INIT_FAILED;
   }
   return INIT_SUCCEEDED;
}

void OnDeinit(const int reason) {
   FileClose(LogHandle);
}

void Log_On_Chart(string labelText, color labelColor) {
   FileWrite(LogHandle, labelText);
   //FileWrite(LogHandle, labelText + "\n"); <-- "\n" is done automatically by FileWrite
   ....
   ....
}
Batuhan Ortay
172
Batuhan Ortay  
lippmaje:

By calling FileOpen you reset the file cursor to the beginning. It's like opening a text in Word and placing the cursor to the start. Then you write a line and close it. The previous line is overwritten.

You need to open and close the file outside the log function like so:


Thanks a lot lippmaje.

To add comments, please log in or register