Problem with while loop and text file
-
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...
-
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...
Hi, your code reads each stream once; i.e. for the first line of the first file, the entire content of the second file is read, then everything comes to a halt since sr2 has reached the end-of-stream condition. if you want to have all combinations of lines from file1 and file2, then you should: - either open the second file inside the while loop reading file1 (hence reopening it for every line of file1); - or reposition sr2 at the beginning over and over again. BTW: Your try-finally construct will swallow any exception that may occur. I recommend you add
catch(Exception exc) {Console.WriteLine(exc.ToString());}
to visualize most problems that may (and sooner or later will) occur. [CORRECTION]Exceptions don't get swallowed by finally blocks, they survive the try-finally construct and will have to be catched at some outer level [/CORRECTION] :)Luc Pattyn [Forum Guidelines] [My Articles]
DISCLAIMER: this message may have been modified by others; it may no longer reflect what I intended, and may contain bad advice; use at your own risk and with extreme care.
modified on Tuesday, June 16, 2009 8:39 AM
-
Hi, your code reads each stream once; i.e. for the first line of the first file, the entire content of the second file is read, then everything comes to a halt since sr2 has reached the end-of-stream condition. if you want to have all combinations of lines from file1 and file2, then you should: - either open the second file inside the while loop reading file1 (hence reopening it for every line of file1); - or reposition sr2 at the beginning over and over again. BTW: Your try-finally construct will swallow any exception that may occur. I recommend you add
catch(Exception exc) {Console.WriteLine(exc.ToString());}
to visualize most problems that may (and sooner or later will) occur. [CORRECTION]Exceptions don't get swallowed by finally blocks, they survive the try-finally construct and will have to be catched at some outer level [/CORRECTION] :)Luc Pattyn [Forum Guidelines] [My Articles]
DISCLAIMER: this message may have been modified by others; it may no longer reflect what I intended, and may contain bad advice; use at your own risk and with extreme care.
modified on Tuesday, June 16, 2009 8:39 AM
Or use ReadAllLines to read each file into a string array and then loop though with nested foreach loops.
No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones
-
Hi, your code reads each stream once; i.e. for the first line of the first file, the entire content of the second file is read, then everything comes to a halt since sr2 has reached the end-of-stream condition. if you want to have all combinations of lines from file1 and file2, then you should: - either open the second file inside the while loop reading file1 (hence reopening it for every line of file1); - or reposition sr2 at the beginning over and over again. BTW: Your try-finally construct will swallow any exception that may occur. I recommend you add
catch(Exception exc) {Console.WriteLine(exc.ToString());}
to visualize most problems that may (and sooner or later will) occur. [CORRECTION]Exceptions don't get swallowed by finally blocks, they survive the try-finally construct and will have to be catched at some outer level [/CORRECTION] :)Luc Pattyn [Forum Guidelines] [My Articles]
DISCLAIMER: this message may have been modified by others; it may no longer reflect what I intended, and may contain bad advice; use at your own risk and with extreme care.
modified on Tuesday, June 16, 2009 8:39 AM
Luc Pattyn wrote:
Your try-finally construct will swallow any exception that may occur
Are you sure? Since the catch is missing, the finally block will be executed and the exception rethrown, no?
-
Luc Pattyn wrote:
Your try-finally construct will swallow any exception that may occur
Are you sure? Since the catch is missing, the finally block will be executed and the exception rethrown, no?
Hi, my mistake, you are right, try-finally without catch does not influence exception throwing/catching at all. :)
Luc Pattyn [Forum Guidelines] [My Articles]
DISCLAIMER: this message may have been modified by others; it may no longer reflect what I intended, and may contain bad advice; use at your own risk and with extreme care.
-
Hi, your code reads each stream once; i.e. for the first line of the first file, the entire content of the second file is read, then everything comes to a halt since sr2 has reached the end-of-stream condition. if you want to have all combinations of lines from file1 and file2, then you should: - either open the second file inside the while loop reading file1 (hence reopening it for every line of file1); - or reposition sr2 at the beginning over and over again. BTW: Your try-finally construct will swallow any exception that may occur. I recommend you add
catch(Exception exc) {Console.WriteLine(exc.ToString());}
to visualize most problems that may (and sooner or later will) occur. [CORRECTION]Exceptions don't get swallowed by finally blocks, they survive the try-finally construct and will have to be catched at some outer level [/CORRECTION] :)Luc Pattyn [Forum Guidelines] [My Articles]
DISCLAIMER: this message may have been modified by others; it may no longer reflect what I intended, and may contain bad advice; use at your own risk and with extreme care.
modified on Tuesday, June 16, 2009 8:39 AM