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?
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.
Related
I am calling this script from a unity project. At this point what I am trying to do is increment the file every time the script is called.
So once it is called it reads the data saves the data in a file.
Right now it only makes a file alpha0.csv and doesn't print a new file if I call the script again.
Can anyone guide me on how to fix this issue.
using UnityEngine;
using System;
using System.Collections;
using System.IO.Ports;
using System.IO;
using System.Management;
using Microsoft.Win32;
using System.Collections.Generic;
public class ArduinoControl : MonoBehaviour
{
SerialPort arduino;
public string portName = "COM5";
public static bool status ;
public string test = "alpha";
private static int counter;
private static int lineCount;
private static string receivedstring = string.Empty;
void Start()
{
arduino = new SerialPort(portName, 115200);
arduino.Open();
status = true;
}
void Update()
{
if (arduino.IsOpen)
{
if (status) // (& UnityCommand = "F")
{
arduino.Write("s");
arduino.ReadTimeout = 5000;
arduino.WriteTimeout = 5000;
receivedstring += arduino.ReadLine() + "\r\n";
arduino.BaseStream.Flush();
lineCount++;
if(lineCount >= 10 && receivedstring != null)
{
WriteOutputToTextFile(test,receivedstring); // Write to csv here...
status = false;
}
arduino.BaseStream.Flush();
}
}
}
//private static int counter;
static void WriteOutputToTextFile(string path,string _data)
{
string FolderName = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); //set destination as your desktop
using (StreamWriter SW = new StreamWriter($"{FolderName }\\{path}{counter}.csv", false))
{
SW.WriteLine(_data);
SW.Close();
}
counter++;
lineCount = 0;
receivedstring = string.Empty;
}
}
If i understand correctly you want to create a new file with the correct numerical suffix, as in alpha0.csv, alpha1.csv, alpha2.csv...
This can actually be a bit of a pain to do, you could use a variable in a wider scope as comments suggest but as you are saving this to disk I assume you want this to work correctly even between starting and stopping your program.
I would suggest
Make sure all your files are in their own folder
Read in all of the file names
Run a 'find max' algorithm on the suffixes in your folder
Add 1 to this and use that to create your new file
Reading in file names can be done in Unity like so:
DirectoryInfo dir = new DirectoryInfo("/*your directory*/");
FileInfo[] fileInfos = dir.GetFiles("*.csv");
Then for getting the next suffix
static readonly string rootName = "Alpha";
int maxFileNumber = 0;
foreach (var f in fileInfos)
{
string tempName = f.Name;
tempName = tempName.Substring(0, tempName.Length - ".csv".Length); // remove .csv
int lengthOfNumber = tempName.Length - rootName.Length; // get the length of the number at the end of the name
nextFileNumber = int.Parse(tempName.Substring(rootName.Length, lengthOfNumber)); // get the number at the end of the name
maxFileNumber = nextFileNumber > maxFileNumber ? nextRoomNumber : maxRoomNumber; // find max alorithm
}
nextFileNumber += 1; // next number
string newFileName = rootName + ToString(nextFileNumber)+".csv";
In my C# console application, using google translate service I am programmatically trying to translate MyResources.resx file into MyResources.fr-CA.resx
Here is the code:
public static string TranslateText(string input, string languagePair)
{
string url = String.Format("http://www.google.com/translate_t?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();
}
static void Main(string[] args)
{
var resWriter = new ResourceWriter("Translations.resources");
ResourceSet resourceSet = MyResources.ResourceManager.GetResourceSet(CultureInfo.CurrentUICulture, true, true);
foreach (DictionaryEntry entry in resourceSet)
{
string resKey = entry.Key.ToString();
string resValue = entry.Value.ToString();
var result = TranslateText(resValue, "en|fr-CA");
if (result != null)
{
resWriter.AddResource(resKey, System.Web.HttpUtility.HtmlDecode(result));
}
}
resWriter.Close();
}
Problem is that some french characters show up as "?" character in resulting Resources.fr-CA.resx file and that also, only if I use System.Web.HttpUtility.HtmlDecode, otherwise it shows characters for example like "d'"
So how can I programmatically insert French based values in a resource file correctly?
You have to change your webClient encoding type to decode French characters properly.
So this line:
webClient.Encoding = System.Text.Encoding.UTF8;
Have to be like this:
webClient.Encoding = System.Text.Encoding.GetEncoding("ISO-8859-1");
and your "result" variable (after calling TranslateText method) should be readable in French correctly.
Please try this way and inform me, it worked for me.
Regards.
I have a small application that checks all of the logs in a directory named after domain usernames and generates a results file with each username and the relevant first and surname for that user.
The console is outputting the full list successfully but it seems that the StreamWriter is stopping halfway through an entry.
The number of characters it writes before stopping is consistent - to some extent. If I set the outputstring to include more characters between the two variables filename and the result from FindNameFromUsername then the character count with or without spaces changes so I've ruled that out.
Any ideas as to why the console outputs the line but the streamwriter doesn't?
Code below:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading.Tasks;
using System.DirectoryServices;
using System.DirectoryServices.AccountManagement;
using System.DirectoryServices.ActiveDirectory;
namespace Filename_Finder
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Please insert the full directory path. All filenames in the root of this folder will be logged to one file.");
string outputFilename = "results.txt";
string userPath = Console.ReadLine();
string domain = "ou=users,dc=domain,dc=local";
try
{
string outputFilepath = userPath + outputFilename;
string[] filepaths = Directory.GetFiles(userPath);
using (StreamWriter file = new StreamWriter(outputFilepath, false))
{
for (int i = 0; i < filepaths.Length; i++)
{
string filepath = filepaths[i];
char split = '.';
string filename = filepath.Remove(0, userPath.Count());
if (filename != outputFilename)
{
int extensionBegins = filename.LastIndexOf(split);
filename = filename.Remove(extensionBegins);
string outputstring = (filename + " -- " + FindNameFromUsername(filename, domain));
Console.WriteLine(outputstring);
file.WriteLine(outputstring);
}
}
Console.ReadLine();
}
}
catch (Exception e) { Console.WriteLine(e); Console.ReadLine(); }
}
public static string FindNameFromUsername(string username, string domainScope)
{
string connectionPrefix = "LDAP://" + domainScope;
DirectoryEntry entry = new DirectoryEntry(connectionPrefix);
DirectorySearcher searcher = new DirectorySearcher(entry);
string filter = "(&(objectClass=user)";
filter += "(|(sAMAccountName=" + username + ")))";
searcher.Filter = filter;
SearchResult result = searcher.FindOne();
string resultValue = string.Empty;
DirectoryEntry obj = result.GetDirectoryEntry();
resultValue = "" + obj.Properties["givenName"].Value + " " + obj.Properties["sn"].Value;
if (resultValue == " ") { resultValue = username; }
entry.Close(); entry.Dispose();
obj.Close(); obj.Dispose();
searcher.Dispose();
return resultValue;
}
}
}
Your Console.ReadLine() will pause the execution and I'm guessing that's where you are checking the content of your file.
However, the file won't be flushed and closed until the StreamWriter is being disposed. That happens at the end of the using block, i.e. after your ReadLine() statement.
Take a look at this question.
You might find useful setting your StreamWriter.AutoFlush property to true in order to get similar behavior to the corresponding Console method, like this:
using (var file = new StreamWriter(outputFilepath, false) { AutoFlush = true })
I have a text file contains several lines with words, for example like this
cards
door
lounge
dog
window
I want to add a new word into that list with the condition that it does not already exist in the list. For example I want to add wind or car
I use File.ReadAllText(#"C:\Temp.txt").Contains(word)
But the problem is window contains wind and cards contain car
Is there any way to compare it uniquely?
If you have not a huge file, you can read it to memory and process it like any array:
var lines = File.ReadAllLines(#"C:\Temp.txt");
if(lines.Any(x=>x == word)
{
//There is a word in the file
}
else
{
//Thee is no word in the file
}
Use File.ReadLine() and check for with String.equals(), dont look for substrings. Something Like this:
while(!reader.EndOfFile0
{
if(String.Compare(reader.ReadLine(),inputString, true) == 0)
{
//do your stuf here
}
}
You should do through Regex match so that you are matching an exact work, and I have made this below as case insensitive.
using ConsoleApplication3;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
public static class Program
{
private static void Main(string[] args)
{
// Read the file and display it line by line.
System.IO.StreamReader file =
new System.IO.StreamReader("c:\\temp\\test.txt");
var line = string.Empty;
string fileData = file.ReadToEnd();
file.Close();
fileData = "newword".InsertSkip(fileData);
StreamWriter fileWrite = new StreamWriter(#"C:\temp\test.txt", false);
fileWrite.Write(fileData);
fileWrite.Flush();
fileWrite.Close();
}
public static string InsertSkip(this string word, string data)
{
var regMatch = #"\b(" + word + #")\b";
Match result = Regex.Match(data, regMatch, RegexOptions.Singleline | RegexOptions.IgnoreCase);
if (result == null || result.Length == 0)
{
data += Environment.NewLine + word;
}
return data;
}
}
Although I am reading an entire file and writing an entire file back. You can improve the performance by only writing one word rather a whole file again.
you can do something like
string newWord = "your new word";
string textFile = System.IO.File.ReadAllText("text file full path");
if (!textFile.Contains(newWord))
{
textFile = textFile + newWord;
System.IO.File.WriteAllText("text file full path",textFile);
}
I have text file which i need to update according to the regex match but as soon as my program tries to write a line into text file it is giving following error..
The process cannot access the file 'D:\Archieve\20140123.text' because it is being used by another process.
Here is my C# code..
static void Main(string[] args)
{
string textfilename="";
string strDateTime = DateTime.Now.ToString("yyyyMMdd");
string strformatedatetime = DateTime.Now.ToString("yyyy/MM/dd");
if (strDateTime != "") {
string loc = "D:\\Archieve\\";
string date=strDateTime;
string text=".text";
textfilename = loc + date + text;
File.Create(textfilename);
}
string pattern = "^" + strformatedatetime + ".*";
string FileToCopy = "D:\\ipdata.txt";
string NewCopy =textfilename;
StringBuilder sb = new StringBuilder("");
List<string> newLines = new List<string>();
if (System.IO.File.Exists(FileToCopy) == true)
{
string[] lines = File.ReadAllLines(FileToCopy);
foreach (string line in lines)
{
if (Regex.IsMatch(line, pattern))
{
sb.Append(line + System.Environment.NewLine);
TextWriter tsw = new StreamWriter(textfilename,true);
//Writing text to the file.
tsw.WriteLine(sb);
//Close the file.
tsw.Close();
}
}
}
}
I am getting above defined error at this line of code...
TextWriter tsw = new StreamWriter(textfilename,true);
Where am i going wrong ?
You don't need to have a separate instruction to create a file.
The StreamWriter will take care of it: Here is the description of the constructor you user
> Initializes a new instance of the StreamWriter class for the specified
> file by using the default encoding and buffer size. If the file
> exists, it can be either overwritten or appended to. If the file does
> not exist, this constructor creates a new file.
Use File.Create(textfilename).Close();
As the error message suggests