私はチャットボットを使って作業しています。私は「学習」機能でコードを作成しようとしています。私はかなり新しくコーディングしていますが、私の問題は読んでいるストリームを閉じないで私に関係していると思われますが、私はどのように考えていません。私はこの難しさを解決する最も効率的な方法を見つけるのを助けたいと思います。ここに私のコードです。代わりに、同じハンドルでそれに読み取り/書き込み、その後、一度File.ReadLines
、File.Open
ファイルを使用しての別のファイルにアクセスしているため、文字列をtxtファイルに書き込めません
class Program
{
static bool isRecording = false;
static bool isTraining = false;
static bool shutdown = false;
static string path = Path.Combine(Directory.GetCurrentDirectory(), "memory.txt");
static string inputValue;
static string outputValue = " ";
static void Main(string[] args)
{
Console.WriteLine("Computer: Hello! I am MyAI Version 2.0.");
Console.WriteLine("Training?");
inputValue = Console.ReadLine();
inputValue = inputValue.ToLower();
inputValue = inputValue.Trim(new Char[] { ' ', '.', ',', ':', ';', '*' });
if (inputValue == "yes")
{
isTraining = true;
Console.WriteLine("Training mode set to true.");
Console.ReadLine();
}
else
{
isTraining = false;
Console.WriteLine("Training mode set to false");
Console.ReadLine();
}
Console.Clear();
CollectInput();
}
static void CollectInput()
{
var lines = System.IO.File.ReadLines(path);
while (!shutdown)
{
Console.Write("User: ");
inputValue = Console.ReadLine();
inputValue = inputValue.ToLower();
inputValue = inputValue.Trim(new Char[] { ' ', '.', ',', ':', ';', '*' });
StringComparison comp = StringComparison.OrdinalIgnoreCase;
if (inputValue == "hi" || inputValue == "hello" || inputValue == "greetings")
{
outputValue = "hi";
}
else if (inputValue.Contains("how are you"))
{
outputValue = "good";
}
else
{
if (isTraining)
{
if (isRecording)
{
//This is where I would like to write inputValue to path
isRecording = false;
}
}
if (File.ReadAllLines(path).Contains(inputValue))
{
outputValue = File.ReadLines(path)
.SkipWhile(line => line != inputValue)
.Skip(1)
.FirstOrDefault();
}
else
{
if (inputValue.Contains("?"))
{
outputValue = inputValue;
//This is where I would like to write inputValue to path
isRecording = true;
}
else
{
outputValue = "what about it?";
}
}
}
Console.Write("Computer: ");
Console.WriteLine(outputValue);
outputValue = " ";
}
}
}
File.Open(path、FileMode.Open); outputValue = File.ReadLines(パス) .SkipWhile(行=>行!=入力値) .Skip(1) .FirstOrDefault(); – Mattinthehat
@Mattinthehatあなたはまだ 'outputValue = File.ReadLines(path)'で新しいファイルハンドルを開いています。 'File.Open'によって返されたハンドルを使用するメソッドを検索します。あなたのIDEやコンパイラは、 'File.Open'の戻り値を破棄していることを警告しなければならないと思います。 –
詳しいことができますか?私はまだ新しいですし、私はコードのその部分をやり直すより良い方法を知らない。基本的にはファイルの入力をチェックし、見つかった場合はその下の行を取ります。 – Mattinthehat