Search code examples
c#stringreader

Out of memory in LineReader


I am making a program that reads a .tyd file and tries to translate all the text between the " from English to Italian.

GoToDesk translate-> "Looking for computer"

The problem is that I am still getting "Out of memory".

The code is:

using System.Collections;
using System.Net;
using MiscUtil.IO;

namespace Soft_inc
{
 class MyProject
{
    public static string TranslateText(string input, string languagePair)
    {
        string url = String.Format("http://translate.google.it/?hl=en&ie=UTF8&text={0}&langpair={1}", input, languagePair);
        WebClient webClient = new WebClient();
        webClient.Encoding = System.Text.Encoding.UTF8;
        string result = webClient.DownloadString(url);
        result = result.Substring(result.IndexOf("<span title=\"") + "<span title=\"".Length);
        result = result.Substring(result.IndexOf(">") + 1);
        result = result.Substring(0, result.IndexOf("</span>"));
        return result.Trim();
}
       public static void Main()
    {
        string path_file = @"H:\Games\Software.In.v11.7.62\Software.In.v11.7.62\Localization\Italiano\idk\UI.tyd";

        string Ftext = System.IO.File.ReadAllText(path_file);
        ArrayList ar = new ArrayList();
    Console.WriteLine("This may require some time.");
    foreach (string line in new LineReader(() => new StringReader(Ftext)))
    {
        if(line.IndexOf("\"") == -1) continue;
        string text = line.Substring(line.IndexOf("\""));
        text = text.Replace("\"","");

        if(text.Length == 0) continue;
        ar.Add(text);
    }

    int idk = 0;

    while(true)
    {
        idk++;
        if(idk == ar.Count) break;
        string oldT = (string)ar[idk];
        Ftext = Ftext.Replace(oldT, TranslateText(oldT,"en|it"));
    }


    System.IO.File.WriteAllText("UI.tyd",Ftext);
}
}


}

Maybe it is because the file has 2535 lines of text? How I can fix this?


Solution

  • You need to use StreamReader class. It is not necessary to read all file content into RAM. Open one StreamReader and one StreamWriter. Reed file line by line and write translated data into a temporary file. When all content is translated just move temp file to needed destination. Don't forget to close source and destination handles before moving.