Search Data from File in FIle2
-
Hi Guys. I have been pulling out the last hair on my head with this one. I have two files, one with a list of numbers and another file containing more data. I want to take the data from file 1 and use that to search file2 for the same value and out put another value from file1 to a new file. I have now tried numerous ways of writing this and it just doesn't work. Below is a snippet of my code. The
foreach
statement doesn't work and if I run it without the foreach statement it only reads the first line of file 1 and doesn't continue to the next line.while (!sr1.EndOfStream) { holdLineHid = sr1.ReadLine(); holdLineNot = sr.ReadToEnd(); if (String.IsNullOrEmpty(holdLineHid) || String.IsNullOrEmpty(holdLineNot)) continue; string hid1 = holdLineHid.Substring(0, 11); string newhid = holdLineHid.Substring(14, 11); string hid2 = holdLineNot.Substring(0, 11); string notremainder = holdLineNot.Substring(11, 683).Trim(); foreach (String hid in holdLineHid) { if (hid1 == hid2) { sw.WriteLine(newhid + notremainder); }
For instance: In File1 I have the following:
C0000000001 C0000013456 C0000000002 C0003245678
In File2 I have the following data:C0000000003N Y Trem Chat C0000000005Y C Just Data C0000000001N D This Must Match
How do I get this to run properly? Thanks in advance.Excellence is doing ordinary things extraordinarily well.
-
Hi Guys. I have been pulling out the last hair on my head with this one. I have two files, one with a list of numbers and another file containing more data. I want to take the data from file 1 and use that to search file2 for the same value and out put another value from file1 to a new file. I have now tried numerous ways of writing this and it just doesn't work. Below is a snippet of my code. The
foreach
statement doesn't work and if I run it without the foreach statement it only reads the first line of file 1 and doesn't continue to the next line.while (!sr1.EndOfStream) { holdLineHid = sr1.ReadLine(); holdLineNot = sr.ReadToEnd(); if (String.IsNullOrEmpty(holdLineHid) || String.IsNullOrEmpty(holdLineNot)) continue; string hid1 = holdLineHid.Substring(0, 11); string newhid = holdLineHid.Substring(14, 11); string hid2 = holdLineNot.Substring(0, 11); string notremainder = holdLineNot.Substring(11, 683).Trim(); foreach (String hid in holdLineHid) { if (hid1 == hid2) { sw.WriteLine(newhid + notremainder); }
For instance: In File1 I have the following:
C0000000001 C0000013456 C0000000002 C0003245678
In File2 I have the following data:C0000000003N Y Trem Chat C0000000005Y C Just Data C0000000001N D This Must Match
How do I get this to run properly? Thanks in advance.Excellence is doing ordinary things extraordinarily well.
-
Hi Guys. I have been pulling out the last hair on my head with this one. I have two files, one with a list of numbers and another file containing more data. I want to take the data from file 1 and use that to search file2 for the same value and out put another value from file1 to a new file. I have now tried numerous ways of writing this and it just doesn't work. Below is a snippet of my code. The
foreach
statement doesn't work and if I run it without the foreach statement it only reads the first line of file 1 and doesn't continue to the next line.while (!sr1.EndOfStream) { holdLineHid = sr1.ReadLine(); holdLineNot = sr.ReadToEnd(); if (String.IsNullOrEmpty(holdLineHid) || String.IsNullOrEmpty(holdLineNot)) continue; string hid1 = holdLineHid.Substring(0, 11); string newhid = holdLineHid.Substring(14, 11); string hid2 = holdLineNot.Substring(0, 11); string notremainder = holdLineNot.Substring(11, 683).Trim(); foreach (String hid in holdLineHid) { if (hid1 == hid2) { sw.WriteLine(newhid + notremainder); }
For instance: In File1 I have the following:
C0000000001 C0000013456 C0000000002 C0003245678
In File2 I have the following data:C0000000003N Y Trem Chat C0000000005Y C Just Data C0000000001N D This Must Match
How do I get this to run properly? Thanks in advance.Excellence is doing ordinary things extraordinarily well.
your foreach will only loop once as your only giving in one string in the holdLineHid (which i assume is a string) what you want to do is create a streamreader for file 1. Read the first line into a string and then create a while loop while not null ...
StreamReader sr = new StreamReader("file1");
string line = sr.ReadLine();
while(line != null)
{
//do something with the line
line = sr.ReadLine();
}sr.close();
Now, in your instance for file 1 you have put two id numbers on each line. Are they one per line in the file? or is there more than one on each line? Then in your while loop, where i put the comment you could do the following...
StreamReader sr2 = new StreamReader("file2");
string line2 = sr2.ReadLine();
while(line2 != null)
{
string hid = line2.Substring(0, 11);if(hid == line1)
{
//Do something with the stream writer
break;//Break the while loop
}line2 = sr2.ReadLine();
}sr2.Close();
That should do what you want
Life goes very fast. Tomorrow, today is already yesterday.
-
The easiest way is too read the file using : string[] lines = File.ReadAllLines(@"c:\myfile.txt"); Then just use: foreach(string line in lines) { //... do what you want here... }
-
your foreach will only loop once as your only giving in one string in the holdLineHid (which i assume is a string) what you want to do is create a streamreader for file 1. Read the first line into a string and then create a while loop while not null ...
StreamReader sr = new StreamReader("file1");
string line = sr.ReadLine();
while(line != null)
{
//do something with the line
line = sr.ReadLine();
}sr.close();
Now, in your instance for file 1 you have put two id numbers on each line. Are they one per line in the file? or is there more than one on each line? Then in your while loop, where i put the comment you could do the following...
StreamReader sr2 = new StreamReader("file2");
string line2 = sr2.ReadLine();
while(line2 != null)
{
string hid = line2.Substring(0, 11);if(hid == line1)
{
//Do something with the stream writer
break;//Break the while loop
}line2 = sr2.ReadLine();
}sr2.Close();
That should do what you want
Life goes very fast. Tomorrow, today is already yesterday.
Hi Mate. I get the jist of this but it seems that the code you have given above puts me in an infinate loop. TO answer your questions:
musefan wrote:
Are they one per line in the file? or is there more than one on each line?
File 1 contains +- 1000 lines with two ID's on each line, the first ID is the one to matched to file2 and the second ID is trhe one to be written to the output file along with the remainder of the data on the matching line of file2. Hope this makes sense. Almost like doing a match in Microsoft Access and then output a combination of the data from the two files.
Excellence is doing ordinary things extraordinarily well.