Reputation: 31
i'm trying to read a text that is separated by a tab '\t' and store it to a list of objects. the text looks like this:
1 Name Number City
i've tried this method but it only works for one line:
string line = sr.ReadLine();
string[] word = line.Split('\t');
for (int i = 0; i <= word.Length; i++)
ec.code = Convert.ToInt32(word[0]);
ec.Name = word[1];
ec.Number = Convert.ToInt32(word[2]);
ec.City = word[3];
list.Add(ec);
How can I read all lines to the list?
Upvotes: 1
Views: 1886
Reputation: 37080
The problem is simply that you're only processing a single line when you do sr.ReadLine()
. Instead, you might consider using File.ReadAllLines
, which reads all the lines into an array. You can combine this with the Split
method to extract the items you need from each line.
Your for loop also doesn't make sense - it appears you're looping over each word in the line, but in the body of the loop you're adding all the words each time. I think the for
loop can be removed.
Also, it's not clear what ec
is, but you should probably be instantiating a new one on each iteration. Otherwise, you're just adding the same reference to your list over and over again, and they will all have the same values (from the last line read).
Here's the sample class that I'm using:
// Class to represent whatever it is you're adding in your loop
class EC
{
public int Code { get; set; }
public string Name { get; set; }
public int Number { get; set; }
public string City { get; set; }
}
One thing we should do is, before referencing indexes into the array returned from calling Split
, we should make sure there are enough items first. Otherwise we will get an IndexOutOfRange
exception if we try to reference an index that doesn't exist.
Also, it's a good idea to make sure that the strings we expect to be integers are actually integers. We can do this by using int.TryParse
, which will return true on success and will set the out argument to the parsed value.
Here's an example that uses all these ideas:
// Path to our file
var filePath = @"f:\public\temp\temp.txt";
// The list of things we want to create from the file
var list = new List<EC>();
// Read the file and create a new EC for each line
foreach (var line in File.ReadAllLines(filePath))
{
string[] words = line.Split('\t');
// First make sure we have enough words to create an EC
if (words.Length > 3)
{
// These will hold the integers parsed from our words (if parsing succeeds)
int code, number;
// Use TryParse for any values we expect to be integers
if (int.TryParse(words[0], out code) && int.TryParse(words[3], out number))
{
list.Add(new EC
{
Code = code,
Name = words[1],
Number = number,
City = words[3]
});
}
}
}
Upvotes: 0
Reputation: 975
Assuming that every line in the file follows your format of 1 Name Number City
, you can try this:
var lines = File.ReadAllLines(filename);
foreach (var line in lines)
{
string[] word = line.Split('\t');
for (int i = 0; i <= word.Length; i++)
{
ec.code = Convert.ToInt32(word[0]);
ec.Name = word[1];
ec.Number = Convert.ToInt32(word[2]);
ec.City = word[3];
list.Add(ec);
}
}
Upvotes: 0