моя первая проблема возникает в том случае, если я объявляю свой файловый поток и т. Д. Таким образом
filestream file;
streamreader file_in;
streamwriter file_out;
try
{
file = new filestream("data.txt", FileMode.OpenOrCreate);
file_in = new streamreader(file);
file_out = new streamwriter(file);
}
catch(IOException exc)
{
Console.WriteLine(exc.Message);
}
выдает ошибку, которая говорит «использование неназначенной локальной переменной», что я нахожу странным, потому что все потокиобъявлен вне блока try, но внутри основного, поэтому они должны существовать внутри основного.
Моя другая проблема возникает в том, что если я удаляю блок try / catch и просто объявляю потоки одной строкой (например,: FileStream file = new FileStream("data.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite);
) мое чтение из файла работает, однако я не могу записать в файл.Моя функция записи в файл выглядит следующим образом:
public bool write_to_file(ref StreamWriter file_out)
{
if (this.is_empty == true)
{
Console.WriteLine("error, there is nothing to write.");
Console.WriteLine("press any key to continue...");
Console.ReadKey();
return false;
}
try
{
string temp = this.is_empty + "," + this.movie_title + "," + this.year_released + "," + this.publisher + "," +
this.length + "," + this.acting_rating + "," + this.music_rating + "," + this.cinematography_rating + "," +
this.plot_rating + "," + this.duration_rating + "," + this.total_rating;
file_out.WriteLine(temp);
return true;
}
catch (IOException exc)
{
Console.WriteLine(exc.Message);
Console.WriteLine("press any key to continue...");
Console.ReadKey();
return false;
}
}
любая помощь будет высоко ценится, спасибо.