I have written a few lines of code to create a c# console application to split a text file into multiple files using the input from the user. This divides the file into lines of 20000 each. I want a user to be able to choose.
I want it to prompt a question “How many lines per file would you like to have?”
For a scenario in which a user has a text file with 200000 lines and wants 1000 lines per file. It will divide the text file into multiples with 1000 lines each. Also another user can want 500 lines per file.
my code looks like this
string fileName = @"C:\Users\COURE-TECH\source\repos\ConsoleApp1\ConsoleApp1\dnd.txt";
var fileSuffix = 0;
int lines = 0;
Stream fstream = File.OpenWrite($"{fileName}" + (++fileSuffix) + ".txt");
StreamWriter sw = new StreamWriter(fstream);
using (var file = File.OpenRead(fileName))
using (var reader = new StreamReader(file))
{
while (!reader.EndOfStream)
{
while (true)
{
Console.WriteLine("How many lines per file would you like to have?");
if (int.TryParse(Console.ReadLine(), out lines))
{
break;
}
Console.WriteLine("Please enter an integer value!");
}
sw.WriteLine(reader.ReadLine());
lines++;
sw.Close();
fstream.Close();
lines = 0;
fstream = File.OpenWrite($"{fileName}"+ (++fileSuffix) + ".txt");
sw = new StreamWriter(fstream);
}
}
sw.Close();
fstream.Close();
Console.WriteLine("Done");
I expect a prompt question “How many lines per file would you like to have?”
And divide the file into the lines the user wants