her is the all of the code:
using System;
using System.IO;
using System.Collections;
namespace readtextfile
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
static void Main(string[] args)
{
}
public void textfiles()
{
try
{
// Create an instance of StreamReader to read from a file.
// The using statement also closes the StreamReader.
using (StreamReader sr = new StreamReader("c:\\q\\test.txt"))
{
string line="";
string nline="";
string mline="123";
bool nextfile = false;
using (StreamWriter sv = new StreamWriter("c:\\q\\" + mline + ".txt"))
//using (StreamWriter sw = new StreamWriter("c:\\q\\" +
mline + ".txt"))
// Read and display lines from the file until the end of
// the file is reached.
while ((line = sr.ReadLine()) != null)
{
nline = line.Substring(0,3);
bool comparing = String.Compare(nline.Substring(0, 3), mline) == 0; //
This is true.
if (comparing == true)
{
if (nextfile == true)
{
sw.WriteLine(line);
}
else
{
sv.WriteLine(line);
nextfile = true;
}
}
else
{
using (StreamWriter sw = new StreamWriter("c:\\q\\" + mline +
".txt"))
sw.WriteLine(line);
nextfile=false;
}
mline = nline;
nextfile=false;
}
}
}
catch (Exception e)
{
// Let the user know what went wrong.
Console.WriteLine("The file could not be read:");
Console.WriteLine(e.Message);
}
}
}
}