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. recieving error in this program

recieving error in this program

Scheduled Pinned Locked Moved C#
databasedata-structureshelp
7 Posts 5 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.
  • N Offline
    N Offline
    Nettai
    wrote on last edited by
    #1

    i am reading datas in text file and write into another text file..but getting exception ... FileStream fs=new FileStream("C:\\Vicky\\DATAS\\1.txt",FileMode.OpenOrCreate,FileAccess.Write); StreamWriter sw = new StreamWriter(fs); FileStream fs1 = new FileStream("c:\\one.txt", FileMode.Open, FileAccess.Read); StreamReader sr = new StreamReader(fs1); string[] s = new string[500]; for(int i = 0; i <= 500; i++) { s[i] = sr.ReadLine(); } for (int j = 0; j <= 500; j++) { sw.WriteLine(s[j]); } sw.Close(); sr.Close(); fs.Close(); fs1.Close(); error is: Index was outside the bounds of the array.in s[i]=sr.readline().....

    T 1 Reply Last reply
    0
    • N Nettai

      i am reading datas in text file and write into another text file..but getting exception ... FileStream fs=new FileStream("C:\\Vicky\\DATAS\\1.txt",FileMode.OpenOrCreate,FileAccess.Write); StreamWriter sw = new StreamWriter(fs); FileStream fs1 = new FileStream("c:\\one.txt", FileMode.Open, FileAccess.Read); StreamReader sr = new StreamReader(fs1); string[] s = new string[500]; for(int i = 0; i <= 500; i++) { s[i] = sr.ReadLine(); } for (int j = 0; j <= 500; j++) { sw.WriteLine(s[j]); } sw.Close(); sr.Close(); fs.Close(); fs1.Close(); error is: Index was outside the bounds of the array.in s[i]=sr.readline().....

      T Offline
      T Offline
      Teuz
      wrote on last edited by
      #2

      An array is 0 based, so 500 elements are indexed from 0 to 499. Just get rid of the = in the for cycles :)

      N 1 Reply Last reply
      0
      • T Teuz

        An array is 0 based, so 500 elements are indexed from 0 to 499. Just get rid of the = in the for cycles :)

        N Offline
        N Offline
        Nettai
        wrote on last edited by
        #3

        thanks man....we got it.....can u explain me wat problem we did it ....explain it briefly.

        T realJSOPR 2 Replies Last reply
        0
        • N Nettai

          thanks man....we got it.....can u explain me wat problem we did it ....explain it briefly.

          T Offline
          T Offline
          Teuz
          wrote on last edited by
          #4

          An array is 0 based. So if the array contains 500 elements the first has index 0 and the last 499. In your for cycle (with <=) at the end you were trying to access to the element with index 500 that obviously is outside the bounds of the array. http://msdn.microsoft.com/en-us/library/9b9dty7d(VS.80).aspx[^] :)

          1 Reply Last reply
          0
          • N Nettai

            thanks man....we got it.....can u explain me wat problem we did it ....explain it briefly.

            realJSOPR Offline
            realJSOPR Offline
            realJSOP
            wrote on last edited by
            #5

            nettai wrote:

            explain it briefly.

            You did it wrong.

            "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
            -----
            "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

            L F 2 Replies Last reply
            0
            • realJSOPR realJSOP

              nettai wrote:

              explain it briefly.

              You did it wrong.

              "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
              -----
              "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

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

              [0,499].Contains(500)==false :)

              Luc Pattyn [Forum Guidelines] [My Articles]


              - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets


              1 Reply Last reply
              0
              • realJSOPR realJSOP

                nettai wrote:

                explain it briefly.

                You did it wrong.

                "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
                -----
                "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

                F Offline
                F Offline
                fred_
                wrote on last edited by
                #7

                sorry man I'll try harder next time plz urgt

                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