First off...thank you all for this forum. I've lurked for quite a while and you've all been so helpful. It's a life saver for someone like me.
Now here's my problem. What I'm trying to do is search each record in a file for an id number. When it finds the number it saves it to production.out. When it doesn't find it it saves it to hold.out.
The id numbers are stored in a separate text file so anyone can edit it.
So I'm reading it into an arraylist then using foreach to iterate through. The problem is it only reads the first number and ignores the others. Does anyone see why?
// CREATE OUTPUT FILES
StreamReader sr2 = new StreamReader("i nput.in");
StreamWriter sw3 = new StreamWriter("p roduction.out") ;
StreamWriter sw4 = new StreamWriter("h old.out");
string line;
// READ SEARCH FILE INTO AN ARRAY
StreamReader sr3 = new StreamReader("S earchFile.txt") ;
ArrayList idNumber = new ArrayList();
string lineval2 = "";
while ((lineval2 = sr3.ReadLine()) != null)
{
idNumber.Add(li neval2);
}
// LOOP THROUGH
foreach ( string compare in idNumber)
{
while ((line = sr2.ReadLine()) != null)
{
if (line.IndexOf(c ompare.ToString ()) == 0)
{
sw3.WriteLine(l ine);
}
else
{
sw4.WriteLine(l ine);
}
}
}
// CLEAN UP
sw3.Flush();
sw3.Close();
sr2.Close();
sw4.Flush();
sw4.Close();
}
}
}
The results of the code above would be something like this. Say the external says this:
id1
id2
id3
It will save all the records that contain id1 into the production.out file and will save everything else into hold.out instead of saving them all to production.out.
Now here's my problem. What I'm trying to do is search each record in a file for an id number. When it finds the number it saves it to production.out. When it doesn't find it it saves it to hold.out.
The id numbers are stored in a separate text file so anyone can edit it.
So I'm reading it into an arraylist then using foreach to iterate through. The problem is it only reads the first number and ignores the others. Does anyone see why?
// CREATE OUTPUT FILES
StreamReader sr2 = new StreamReader("i nput.in");
StreamWriter sw3 = new StreamWriter("p roduction.out") ;
StreamWriter sw4 = new StreamWriter("h old.out");
string line;
// READ SEARCH FILE INTO AN ARRAY
StreamReader sr3 = new StreamReader("S earchFile.txt") ;
ArrayList idNumber = new ArrayList();
string lineval2 = "";
while ((lineval2 = sr3.ReadLine()) != null)
{
idNumber.Add(li neval2);
}
// LOOP THROUGH
foreach ( string compare in idNumber)
{
while ((line = sr2.ReadLine()) != null)
{
if (line.IndexOf(c ompare.ToString ()) == 0)
{
sw3.WriteLine(l ine);
}
else
{
sw4.WriteLine(l ine);
}
}
}
// CLEAN UP
sw3.Flush();
sw3.Close();
sr2.Close();
sw4.Flush();
sw4.Close();
}
}
}
The results of the code above would be something like this. Say the external says this:
id1
id2
id3
It will save all the records that contain id1 into the production.out file and will save everything else into hold.out instead of saving them all to production.out.
Comment