I am trying to make a dictionary where the compiler reads from a text file where there are two words per line. I have parsed it through the split() method but I am struggling on how to add the corresponding keys and values from the line to the dictionary container. I am trying to add it in the ReadStream2() function after doing the split() in the line line.add(words[0],words[1]). I know this is wrong but I have no idea how to combine what I am parsing into the dictionary in terms of keys and values.Also on the part where I write the askWord() function I am trying to state that if a line key matches with user input it will output the value(translation) of the word. But the line object of the dictionary I created does not pick up keys and values. Thanks!
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Project_Dictionary
{
class Program
{
static void Main(string[] args)
{
Dictionary<string, string> line = new Dictionary<string, string>();
FileStream filestream = null;
string path = "Dictionary.txt";
Menu(filestream, path);
ReadStream2(filestream,path);
Group(filestream, path);
}
static void WriteByte(FileStream filestream, string path)
{
string str;
Console.WriteLine("Enter word");
str = Console.ReadLine();
try
{
filestream = new FileStream("Dictionary.txt", FileMode.Open, FileAccess.Write);
byte[] by = Encoding.Default.GetBytes(str);
filestream.Write(by, 0, by.Length);
Console.WriteLine("File written");
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
filestream.Close();
}
}
static void ReadByte(FileStream filestream, string path)
{
try
{
filestream = new FileStream(path, FileMode.Open, FileAccess.Read);
byte[] by = new byte[(int)filestream.Length];
filestream.Read(by, 0, by.Length);
string str = Encoding.Default.GetString(by);
Console.WriteLine("File read");
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
filestream.Close();
}
}
static void WriteStream(FileStream filestream, string path)
{
using (filestream = new FileStream(path, FileMode.Append, FileAccess.Write))
{
using (StreamWriter streamWriter = new StreamWriter(filestream))
{
}
}
}
static void ReadStream2(FileStream fileStream, string path)
{
using (fileStream = new FileStream(path, FileMode.Open, FileAccess.Read))
{
Dictionary<string, string> line = new Dictionary<string, string>();
using (StreamReader sw = new StreamReader(fileStream))
{
string rez = "";
while(sw.Peek() > 0)
{
rez = sw.ReadLine();
Console.WriteLine(rez);
string[] words = rez.Split(' ');
line.Add(words[0], words[1]);
}
}
}
}
static void Group(FileStream fileStream, string path)
{
var line = File
.ReadLines(path)
.Select((v, i) => new { Index = i, Value = v })
.GroupBy(p => p.Index / 2)
.ToDictionary(g => g.First().Value, g => g.Last().Value);
}
static void Menu(FileStream fileStream, string path)
{
char choice;
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Welcome this is a English-Ukrainian dictionary press d to continue");
Console.ResetColor();
choice = Convert.ToChar(Console.ReadLine());
while (choice == 'd' || choice == 'D')
{
ReadStream2(fileStream, path);
}
}
static void askWord()
{
string ask;
Console.WriteLine("What english word would you like to translate");
ask = Console.ReadLine();
if (ask == )
}
}
}
What I have tried:
I have tried combining it all but I do not know how to proceed in tying it into the dictionary container and then extracting the corresponding keys and values.