Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Problem with while loop and text file

Problem with while loop and text file

Scheduled Pinned Locked Moved C#
help
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    kallileo
    wrote on last edited by
    #1

    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...

    L 1 Reply Last reply
    0
    • K kallileo

      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...

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      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

      OriginalGriffO L K 3 Replies Last reply
      0
      • L Luc Pattyn

        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

        OriginalGriffO Offline
        OriginalGriffO Offline
        OriginalGriff
        wrote on last edited by
        #3

        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

        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
        "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

        1 Reply Last reply
        0
        • L Luc Pattyn

          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

          L Offline
          L Offline
          Le centriste
          wrote on last edited by
          #4

          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?

          L 1 Reply Last reply
          0
          • L Le centriste

            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?

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #5

            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.


            1 Reply Last reply
            0
            • L Luc Pattyn

              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

              K Offline
              K Offline
              kallileo
              wrote on last edited by
              #6

              Thank you guys... I did what Luc said and it worked.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups