feat: Finish DelteLine
Some checks failed
Tests / Unit Testing1 (push) Failing after 43s

This commit is contained in:
Louis Gallet 2023-11-02 21:15:28 +01:00
parent f90af7320f
commit 29bc7e16cc
Signed by: lgallet
SSH Key Fingerprint: SHA256:qnW7pk4EoMRR0UftZLZQKSMUImbEFsiruLC7jbCHJAY
3 changed files with 42 additions and 1 deletions

View File

@ -1,3 +1,3 @@
// See https://aka.ms/new-console-template for more information
Stream.Stream.MyReplace("test.txt", 'u', 'a');
Stream.Stream.MyReplace("testsqdmfljqsfmlj.txt", 'u', 'a');

View File

@ -41,4 +41,36 @@ public class Stream
throw new ArgumentException("Error");
}
}
public static void DeleteLines(string path, int n)
{
string temp = String.Empty;
string final = String.Empty;
int i = 0;
try
{
using (StreamReader sr = new StreamReader(path))
{
string? line;
while ((line = sr.ReadLine()) != null)
{
if (i % n != 0)
{
final += line + "\n";
}
i++;
}
using (StreamWriter sw = new StreamWriter(path))
{
sw.Write(final);
}
}
}
catch (Exception)
{
throw new ArgumentException("Error");
}
}
}

View File

@ -19,4 +19,13 @@ public class StreamTest
global::Stream.Stream.MyReplace(filePath, toReplace, replace);
Assert.Equal("Cfstvraimfntmagnifiquf\n", File.ReadAllText(filePath));
}
[Theory]
[InlineData("DeleteLineTestFile.txt", 2)]
public void DeleteLineTest(string filePath, int n)
{
global::Stream.Stream.DeleteLines(filePath, n);
Assert.Equal("1\n3\n5\n7\n9\n11\n", File.ReadAllText(filePath));
}
}