|
-
Jul 19th, 2012, 11:01 AM
#1
Thread Starter
Lively Member
Creating lines of a text file into an array?
C# Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace CompareRegistryFiles
{
class Program
{
static void Main(string[] args)
{
string sDesktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
string sFilePath = sDesktopPath + "\\" + "3.txt";
using (StreamReader oStreamReader = new StreamReader(sFilePath))
{
string line;
string[] arr = new String[3];
int x = 0;
while ((line = oStreamReader.ReadLine()) != null)
{
// 3.txt
// a
// b
// [space]
// d
Console.WriteLine(x + " " + line);
// if string[] arr = new String[3]
// error: Index was outside the bounds of the array.
arr[x] = line;
x++;
}
Console.ReadLine();
}
}
}
}
Please see the comments above.
If
if string[] arr = new String[4],
no error.
But there is only 4 lines and the arr index x starts from 0. So the size of arr should be no more than 3.
Could anyone explain to me why the error occurs?
Thanks.
-
Jul 19th, 2012, 11:06 AM
#2
Fanatic Member
Re: Creating lines of a text file into an array?
 Originally Posted by MSDN
Arrays are zero indexed: an array with n elements is indexed from 0 to n-1.
http://msdn.microsoft.com/en-us/library/9b9dty7d.aspx
-
Jul 19th, 2012, 11:10 AM
#3
Re: Creating lines of a text file into an array?
Declaring an array of size 4 allows for indices {0, 1, 2, 3}.
This may sound illogical, but upon declaring an array you don't specify the upper bound; rather the number of variables, the array will be able to store. And with a zero-based array indexing like in C, arrays allways have highest index equal to the length - 1. You can verify this with the GetUpperBound method.
Tom
#EDIT: Slow typing...
In truth, a mature man who uses hair-oil, unless medicinally , that man has probably got a quoggy spot in him somewhere. As a general rule, he can't amount to much in his totality. (Melville: Moby Dick)
-
Jul 19th, 2012, 11:12 AM
#4
Fanatic Member
Re: Creating lines of a text file into an array?
 Originally Posted by ThomasJohnsen
#EDIT: Slow typing...
But more explanatory
-
Jul 19th, 2012, 11:26 AM
#5
Thread Starter
Lively Member
Re: Creating lines of a text file into an array?
 Originally Posted by ThomasJohnsen
Declaring an array of size 4 allows for indices {0, 1, 2, 3}.
#EDIT: Slow typing...
Can you tell me how to return the indices?
Say:
return something like this from an array?
-
Jul 19th, 2012, 11:33 AM
#6
Fanatic Member
Re: Creating lines of a text file into an array?
 Originally Posted by tinfanide
Can you tell me how to return the indices?
Say:
return something like this from an array?
Why would you need the numbering of indices? If you do GetUpperBound() on array, you get the max index and then you know your array indices are from 0 to that number. Or you can also do arrayVariable.Length that will give you the total number of elements present in the array.
-
Jul 19th, 2012, 11:38 AM
#7
Re: Creating lines of a text file into an array?
Well the method {your array name}.GetLowerBound will return 0 and the method {your array name}.GetUpperBound will return 3.
If you want to loop safely through all the items in an array, you can use foreach instead of for (omitting the use of bounds). And there are ways to read all lines of a textfile into an array with a single command (assuming your file is non-binary, you could for example use: ReadAllLines (http://msdn.microsoft.com/en-us/libr...dalllines.aspx)).
Tom
#EDIT: Slow twice .... no yellow jersey for me.
In truth, a mature man who uses hair-oil, unless medicinally , that man has probably got a quoggy spot in him somewhere. As a general rule, he can't amount to much in his totality. (Melville: Moby Dick)
-
Jul 19th, 2012, 11:53 AM
#8
Thread Starter
Lively Member
Re: Creating lines of a text file into an array?
 Originally Posted by ThomasJohnsen
Declaring an array of size 4 allows for indices {0, 1, 2, 3}.
This may sound illogical, but upon declaring an array you don't specify the upper bound; rather the number of variables, the array will be able to store. And with a zero-based array indexing like in C, arrays allways have highest index equal to the length - 1. You can verify this with the GetUpperBound method.
Tom
#EDIT: Slow typing...
 Originally Posted by rjv_rnjn
Why would you need the numbering of indices? If you do GetUpperBound() on array, you get the max index and then you know your array indices are from 0 to that number. Or you can also do arrayVariable.Length that will give you the total number of elements present in the array.
No, there's no need doing so. After some experiments with what ya all have just said, I quite get what you mean by that. I know arrays start from 0 in some other languages. But just got messed up in C#, though.
-
Jul 19th, 2012, 11:56 AM
#9
Thread Starter
Lively Member
Re: Creating lines of a text file into an array?
 Originally Posted by ThomasJohnsen
Well the method {your array name}.GetLowerBound will return 0 and the method {your array name}.GetUpperBound will return 3.
If you want to loop safely through all the items in an array, you can use foreach instead of for (omitting the use of bounds). And there are ways to read all lines of a textfile into an array with a single command (assuming your file is non-binary, you could for example use: ReadAllLines ( http://msdn.microsoft.com/en-us/libr...dalllines.aspx)).
Tom
#EDIT: Slow twice .... no yellow jersey for me.
Really feel grateful for the reference given by ya. Really helpful.
The use of
Code:
string readText = File.ReadAllLines(sFileName);
resolved all my questions in my head cos
I was just about to ask if
in C#
we could or not declare an array without a size.
-
Jul 19th, 2012, 01:26 PM
#10
Fanatic Member
Re: Creating lines of a text file into an array?
 Originally Posted by tinfanide
The use of
Code:
string readText = File.ReadAllLines(sFileName);
resolved all my questions in my head cos
I was just about to ask if
in C#
we could or not declare an array without a size.
In most cases ReadAllLines() will suffice. But this can come back if your file sizes are too big. Say if you are reading a file of 100MB, you would not want to read the entire file in memory at once. In that case you may want to read that line by line and store each line in a list.
You can store your lines as a List of strings. Something like,
Code:
List<string> fileLines = new List<string>();
while ((line = oStreamReader.ReadLine()) != null)
{
fileLines.Add(line);
}
I know this may be a bit too much to begin with but keep in mind to check Generics later.
There are a whole list of collection classes in .Net that you can use where you don't have to specify the length of the collection in advance.
http://msdn.microsoft.com/en-us/library/6tc79sx1.aspx
-
Jul 20th, 2012, 01:44 AM
#11
Thread Starter
Lively Member
Re: Creating lines of a text file into an array?
 Originally Posted by rjv_rnjn
In most cases ReadAllLines() will suffice. But this can come back if your file sizes are too big. Say if you are reading a file of 100MB, you would not want to read the entire file in memory at once. In that case you may want to read that line by line and store each line in a list.
You can store your lines as a List of strings. Something like,
Code:
List<string> fileLines = new List<string>();
while ((line = oStreamReader.ReadLine()) != null)
{
fileLines.Add(line);
}
I know this may be a bit too much to begin with but keep in mind to check Generics later.
There are a whole list of collection classes in .Net that you can use where you don't have to specify the length of the collection in advance.
http://msdn.microsoft.com/en-us/library/6tc79sx1.aspx
Yes, it does sound a bit too much for me to begin with.
ArrayList, StringCollection, List, Enumerator...
Each of them seems slightly different from one another.
But anyway, it's always good to have people here refer me to some references.
Thanks.
-
Jul 21st, 2012, 02:32 PM
#12
Re: Creating lines of a text file into an array?
 Originally Posted by rjv_rnjn
In most cases ReadAllLines() will suffice. But this can come back if your file sizes are too big. Say if you are reading a file of 100MB, you would not want to read the entire file in memory at once. In that case you may want to read that line by line and store each line in a list.
You can store your lines as a List of strings. Something like,
Code:
List<string> fileLines = new List<string>();
while ((line = oStreamReader.ReadLine()) != null)
{
fileLines.Add(line);
}
I know this may be a bit too much to begin with but keep in mind to check Generics later.
There are a whole list of collection classes in .Net that you can use where you don't have to specify the length of the collection in advance.
http://msdn.microsoft.com/en-us/library/6tc79sx1.aspx
I would use a List<T> capacity if that were the case, this would still load ~100MB into memory, which may not be what you want, load items up to the capacity, then deal with them and continue loading up to the capacity, then repeat.
<<<------------
.NET Programming (2012 - 2018)
®Crestron - DMC-T Certified Programmer | Software Developer <<<------------
-
Jul 22nd, 2012, 09:34 PM
#13
Thread Starter
Lively Member
Re: Creating lines of a text file into an array?
 Originally Posted by AceInfinity
I would use a List<T> capacity if that were the case, this would still load ~100MB into memory, which may not be what you want, load items up to the capacity, then deal with them and continue loading up to the capacity, then repeat.
Yes.
But I would like to ask... in the example shown in this reference:
http://msdn.microsoft.com/en-us/library/y52x03h2.aspx
The .Count refers to the actual size of the List items. But why is the Capacity up to 8 (where the actual size of the List in that example is 5 only)?
-
Jul 23rd, 2012, 09:27 AM
#14
Fanatic Member
Re: Creating lines of a text file into an array?
 Originally Posted by tinfanide
Since MSDN does not say anything on this, I would guess that's non-deterministic or maybe there is some algo used by BCL to increment the capacity based on the number of elements the List contains.
-
Jul 23rd, 2012, 09:32 AM
#15
Fanatic Member
Re: Creating lines of a text file into an array?
 Originally Posted by AceInfinity
I would use a List<T> capacity if that were the case, this would still load ~100MB into memory, which may not be what you want, load items up to the capacity, then deal with them and continue loading up to the capacity, then repeat.
My point was read a line, do the processing on the line and then read next line. And how do you suggest to achieve reading the file partially? Or did you mean something else? My point is, as far as I know, if you are reading a file, either you read it whole into the memory or read it line by line. I do not know of a way to read partial file, process the lines read, and then read from the last file location offset till next.
-
Jul 23rd, 2012, 09:16 PM
#16
Thread Starter
Lively Member
Re: Creating lines of a text file into an array?
-
Jul 25th, 2012, 09:30 PM
#17
Re: Creating lines of a text file into an array?
 Originally Posted by rjv_rnjn
My point was read a line, do the processing on the line and then read next line. And how do you suggest to achieve reading the file partially? Or did you mean something else? My point is, as far as I know, if you are reading a file, either you read it whole into the memory or read it line by line. I do not know of a way to read partial file, process the lines read, and then read from the last file location offset till next.
You shouldn't have to store the full thing into a list. If you want it to contain more than one value, then by 'buffer' I mean setting a capacity of items (to make sure that our list doesn't exceed a certain number of elements), and clearing the list when capacity is reached to start adding items from index 0 into the array again, as the StreamReader continues to read each line one by one.
If you don't need this then you don't have to store anything, deal with the lines one by one, do what you need to do with each line (string), and keep reading.
Here's pseudo logic of what i'm talking about:
1) A file with 13 lines (odd number for easier understanding of a possible problem that I foolishly forgot about the first time I tried this).
2) Set a StreamReader instance with a capacity of 2
3) Read through the lines of this text file adding each to the List as usual
4) if we reach our capacity then deal with the data in the list
5) CLEAR it...
6) Continue reading
7) Repeat
That way we're not continuously dealing with this data if you don't store anything. But you're also not storing the entire collection of lines into a List before dealing with the data.
But note this...
8) Once the StreamReader is finished reading, in this case we would never reach the capacity here because 13 is not divisible by 2. So we check to see if the list contains anything leftover from the end of the file, and deal with whatever is left over, if anything.
Now we can get rid of the list all together if you want.
EDIT:
vb Code:
Dim MyList As New List(Of String)(5) Using sr As New StreamReader("C:\Myfile.txt") While sr.Peek < -1 While MyList.Count < MyList.Capacity MyList.Add(sr.ReadLine) End While 'Deal with contents of MyList here MyList.Clear() End While End Using If MyList.Count > 0 Then 'Deal with remaining contents of MyList here MyList.Clear() 'Optional, but for this example there's no further purpose for MyList End If
Perhaps useful for reading and writing modifications in partial? Without dealing with all data at once? I don't know, but this is what I was trying to explain earlier whether somebody finds a use for this strategy here or not.
("Buffer" indicated by the capacity of 5 here. I could have put it into a separate variable to store it's numerical value to be placed within the capacity brackets there, but I wrote this quick and I don't see an urgent need for that for just an example.)
Cheers
~Ace
Last edited by AceInfinity; Jul 25th, 2012 at 09:50 PM.
<<<------------
.NET Programming (2012 - 2018)
®Crestron - DMC-T Certified Programmer | Software Developer <<<------------
-
Jul 26th, 2012, 08:18 AM
#18
Fanatic Member
Re: Creating lines of a text file into an array?
Ok, I see what you meant.
-
Jul 27th, 2012, 09:43 AM
#19
Re: Creating lines of a text file into an array?
 Originally Posted by tinfanide
Yes.
But I would like to ask... in the example shown in this reference:
http://msdn.microsoft.com/en-us/library/y52x03h2.aspx
The .Count refers to the actual size of the List items. But why is the Capacity up to 8 (where the actual size of the List in that example is 5 only)?
The capacity will be doubled when it is exceeded. Thus a list with 167 elements will have capacity of 256 elements - a list with 319 elements will have capacity of 512 elements. This is completely deliberate to ensure that insertions into the list will be roughly O(1).
Tom
In truth, a mature man who uses hair-oil, unless medicinally , that man has probably got a quoggy spot in him somewhere. As a general rule, he can't amount to much in his totality. (Melville: Moby Dick)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|