Заменить содержимое самого короткого файла на содержимое самого длинного - C# (188448)
Формулировка задачи:
Даны три файла одного и того же типа, но разного размера. Заменить содержимое самого короткого файла на содержимое самого длинного. Где тут ошибка ?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("file45");
FileStream file1 = new FileStream(@"C:\1.txt", FileMode.Append);
StreamWriter f1 = new StreamWriter(file1);
FileStream file2 = new FileStream(@"C:\2.txt", FileMode.Append);
StreamWriter f2 = new StreamWriter(file2);
FileStream file3 = new FileStream(@"C:\3.txt", FileMode.Append);
StreamWriter f3 = new StreamWriter(file2);
string str1 = File.ReadAllText(@"C:\1.txt");
string str2 = File.ReadAllText(@"C:\2.txt");
string str3 = File.ReadAllText(@"C:\3.txt");
if (str1.Length > str2.Length && str1.Length > str3.Length)
{
f1.Write(str1);
f2.Write(str1);
f3.Write(str1);
}
if (str2.Length > str1.Length && str2.Length > str3.Length)
{
f1.Write(str2);
f2.Write(str2);
f3.Write(str2);
}
if (str3.Length > str1.Length && str3.Length > str2.Length)
{
f1.Write(str3);
f2.Write(str3);
f3.Write(str3);
}
f1.Close();
f2.Close();
f3.Close();
}
}
}Решение задачи: «Заменить содержимое самого короткого файла на содержимое самого длинного»
textual
Листинг программы
string[] files = { "1.txt", "2.txt", "3.txt" };
FileInfo minLFile = new FileInfo(files[0]);
FileInfo maxLFile = new FileInfo(files[0]);
for (int i = 1; i < files.Length; i++) {
FileInfo fi = new FileInfo(files[i]);
if (minLFile.Length >= fi.Length) minLFile = fi;
if (maxLFile.Length <= fi.Length) maxLFile = fi;
}
if (maxLFile.Name == minLFile.Name) Console.WriteLine("Один и тот же файл!");
else maxLFile.CopyTo(minLFile.Name, true);