recieving error in this program
-
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().....
-
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().....
-
An array is 0 based, so 500 elements are indexed from 0 to 499. Just get rid of the = in the for cycles :)
-
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[^] :)
-
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 -
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[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
-
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