so i am loading a file that has some encrypted text in it, it uses a custom character table, how can i load it from an external file or put the character table in the code ?
Thank you.
so i am loading a file that has some encrypted text in it, it uses a custom character table, how can i load it from an external file or put the character table in the code ?
Thank you.
Start by going over the file and counting the lines so you can allocate an array. You could just use a list here but arrays have much better performance and you have a significant amount of items which you'll have to loop over a lot (once for each encoded char in the file) so I think you should use an array instead.
int lines = 0;
try
{
using (StreamReader sr = new StreamReader("Encoding.txt"))
{
string line;
while ((line = sr.ReadLine()) != null)
{
lines++;
}
}
}
catch (Exception e)
{
// Let the user know what went wrong.
Console.WriteLine("The file could not be read:");
Console.WriteLine(e.Message);
}
Now we're going to allocate and array of tuples;
Tuple<string, string> tuples = new Tuple<string, string>[lines];
After that we'll loop over the file again adding each key-value pair as a tuple.
try
{
using (StreamReader sr = new StreamReader("Encoding.txt"))
{
string line;
for (int i =0; i < lines; i++)
{
line = sr.Readline();
if (!line.startsWith('#')) //ignore comments
{
string[] tokens = line.Split('='); //split for key and value
foreach(string token in tokens)
token.Trim(' '); // remove whitespaces
tuples[i].Item1 = tokens[0];
tuples[i].Item2 = tokens[1];
}
}
}
}
catch (Exception e)
{
// Let the user know what went wrong.
Console.WriteLine("The file could not be read:");
Console.WriteLine(e.Message);
}
I've given you a lot of code although this may take a little tinkering to make work. I didn't both to write the second loop in a compiler and I'm too lazy to look up things like System.String.Trim and make sure I'm using it correctly. I'll leave those things to you. This has the core logic to do it. If you want to instead use a list move the logic inside of the for loop into the while loop where I count the lines.
Do decode the file you're reading you'll have to loop over this array and compare the keys or values until you have a match.
One other thing - your array of tuples is going to have some empty indexes (the array is of length lines while there are actually lines - comments + blankLines in the file). You'll need some check to make sure you're not accessing these indexes when you try to match characters. Alternatively, you could enhance the file reading so it doesn't count blank lines or comments or remove those lines from the file you read from. The best solution would be to enhance the file reading but that's also the most work.
s of custom chars you'll use a nested loop to go over s matching each char with one in the array.