humanemulator.net — справка. Продукт, демо и покупка — на хуманэмулятор.рф | Демо | Купить | API | лог изменений

add_string_to_file

add_string_to_file($file,$str,$timeout=COMMAND_TIME); - добавить заданную строку в файл

Функция на вход принимает параметры:

  • $file – путь к файлу, в который надо добавить строку. Может быть как абсолютный так и относительный (относительно папки скрипта).
  • $str – строка, которую надо добавить в файл
  • $timeout – максимальное время на выполнение этой операции (в секундах), по умолчанию - 60 сек, если за это время операция не завершится, то скрипт продолжает выполнятся дальше.

    После отработки функция возвращает результат своей работы в скрипт :
  • true – операция выполнена (boolean)
  • false – операция не выполнена (boolean)

    Пример использования
    <?php 
    
    // Scenario: Add multiple strings to a text file and display the result
    $xhe_host = "localhost:7010";
    // Connect functional objects if not already connected
    if (!isset($path)){
      $path = "../../../Templates/init.php";
      require($path);
    }
    // info
    echo "\n<span >textfile->" . basename (__FILE__) . "</span>\n";
    
    
    // Example 1: Add strings to file
    // Set file path
    $filePath = "test/add_string.txt";
    //$filePath = "D:/add_string.txt";
    // Set timeout
    $timeout = 60;
    
    echo("1. Add strings to file : \n\n");
    for ($k=0;$k<10;$k++)
    {
    	// Set string to add
    	$stringToAdd = "string_ №".$k."\r\n";
    	
    	// Add string to file
    	$result = SYSTEM::$textfile->add_string_to_file($filePath, $stringToAdd, $timeout);
    	
    	// Check result and echo appropriate message
    	if ($result) {
    		echo ("Added string: string №".$k." : true\n");
    	} else {
    		echo ("Added string: string №".$k." : false\n");
    	}
    }
    
    
    // End
    echo "\n";
    
    // Quit the application
    WINDOW::$app->quit();
    ?>
    # Additional paths
    import sys
    sys.path.insert(0, '../../../Templates PY/')
    
    xhe_host = "127.0.0.1:7010"
    from xweb_human_emulator import *
    
    # начало
    echo("<hr><font color=blue>textfile.xxxxxxxxx</font><hr>")
    
    # 1 
    file = "test\\add_string.txt"
    echo("1. Добавляем строки в файл : \n\n")
    for i in range(0,9):
    	echo("Добавили строку: строка №"+str(i)," : ")
    	echo(textfile.add_string_to_file(file,"строка_ №"+str(i)+"\r\n",60),"<br>")
    
    # покажем результаты
    app.shell_execute("",file)
    
    # конец
    echo("<hr><br>")
    
    # Quit
    app.quit()
    #region using
    
    using System;
    using System.Diagnostics;
    using System.Collections.Generic;
    using System.Linq;
    using System.IO;
    using System.Text;
    using System.Threading;
    
    using XHE;
    using XHE.XHE_DOM;
    using XHE.XHE_System;
    using XHE.XHE_Window;
    using XHE.XHE_Web;
    
    #endregion
    
     class Program:XHEScript
     {
    	  static void Main(string[] args)
    	  {
    			// init XHE
    			server = "localhost:7010";
    			InitXHE();
    
    			// начало
    			echo("<hr><font color=blue>textfile.get_all_external_inner_texts_and_hrefs</font><hr>");
    				
    			// 1 шаг
    			string file = "test\\add_string.txt";
    			echo ("1. Добавляем строки в файл : \n\n");
    			for (int i=0;i<10;i++)
    			{
    				echo ("Добавили строку: строка №"+i+" : ");
    				echo (textfile.add_string_to_file(file,"строка №"+i+"\r\n",60)+"<br>");
    			}
    
    			// покажем результаты
    			app.shell_execute("",file);
    
    			// конец
    			echo("\n\n");
    
    			app.quit();            
    	  }
    }
    // подключим функциональные объекты, если еще не подключен
    xhe_host="127.0.0.1:7010";
    echo=require("../../../Templates JS/init.js");
    
    // начало
    echo("<hr><font color=blue>textfile.add_string_to_file</font><hr>");
    
    // 1 шаг
    var file = "test\\add_string.txt";
    echo("1. Добавляем строки в файл : \n\n");
    for (i=0;i<10;i++)
    {
    	echo ("Добавили строку: строка №"+i+" : ");
    	echo (textfile.add_string_to_file(file,"строка №"+i+"\r\n",60)+"<br>");
    }
    
    // покажем результаты
    app.shell_execute("",file);
    
    // конец
    echo("\n");
    
    // Quit
    app.quit();

    =============================================
    Textfile    Объекты    DOM  System  Vision  Web  Window        
    =============================================
    если что-то непонятно или необходимо узнать или считаете что надо добавить по работе этой функции, пишите в комментарии или на наш форум
    .