المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : Problem with while loop and text file



C# Programming
06-16-2009, 03:12 PM
I have 2 text files.

input1.txt:
560-005;CORFU_1B;560-005-011
560-005;CORFU_1E;560-005-012
560-005;CORFU_1Z;560-005-013
560-005;CORFU_1H;560-005-014
560-005;SENSE_2B;560-005-021

input2.txt
562-Z21;METAL-ANTHRACITE;562-121-000
562-Z21;METAL-9007;562-221-000
562-Z22;METAL-ANTHRACITE;562-122-000
562-Z22;METAL-9007;562-222-000

public void Conversion()
{
try
{
using (sr1 = new StreamReader("c:\\input1.txt"))
{
using (sr2 = new StreamReader("c:\\input2.txt"))
{
file = new FileStream("c:\\output.txt", FileMode.Create, FileAccess.Write);
sw = new StreamWriter(file);
string line1;
string line2;

while ((line1 = sr1.ReadLine()) != null)
{
string[] temp1 = line1.Split(new char[] { ';' });

while ((line2 = sr2.ReadLine()) != null)
{
string[] temp2 = line2.Split(new char[] { ';' });

string final = temp1[0] + ";" + temp1[1] + ";" + temp1[2] + ";" + temp2[2] + ";" + temp1[2] + "-" + temp2[2];
sw.WriteLine(final);
}
}

}
}
}
finally
{
sr1.Close();
sr2.Close();
sw.Close();
file.Close();
}
}

I have created the above method to do some string management. The problem is that the outer while loops runs only for the first line in input1.txt and I don't know why. It's probably something simple but I can't find the solution.

Thanks...