Discussion:
Text File Reading & Writing
(too old to reply)
G. B
2018-06-03 16:00:00 UTC
Permalink
Why is this program giving me this error when you run it?
Unhandled Exception: System.IndexOutOfRangeException: Index was
outside the bounds of the array.
at Program.Main()
The text file is very simple like like so:

Joe,Blog,www.joeblog.com
Joe,Smith,www.joesmith.com
Sue,Blog,www.sueblog.com



<========= The program starts here =========>
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
class Program
{
static void Main()
{
// // string[] lines = File.ReadAllLines (filePath);
// List<string> lines = File.ReadAllLines (filePath).ToList();
// foreach (string line in lines)
// {
// Console.WriteLine( line );
// }
// lines.Add("Sue,Storm,www.stormy.com");
// File.WriteAllLines (filePath, lines);
List<Person> people = new List<Person>();
List<string> lines = File.ReadAllLines(filePath).ToList();
foreach (var line in lines)
{
string[] entries = line.Split(',');
Person newPerson = new Person();
newPerson.FirstName = entries[0];
newPerson.LastName = entries[1];
newPerson.Url = entries[2];
people.Add(newPerson);
}
Console.WriteLine("Read from text file\n");
foreach (var person in people)
{
{person.Url}");
}
people.Add(new Person { FirstName = "Greg", LastName =
"Jones", Url = "www.gregjones.com" });
List<string> output = new List<string>();
foreach (var person in people)
{
output.Add($"{person.FirstName},{person.LastName},{person.Url}");
}
Console.WriteLine("Writing to text file ...\n");
File.WriteAllLines(filePath, output);
Console.WriteLine("All entries written ...\n");
Console.ReadKey();
}
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Url { get; set; }
}
}
<========= The program ends here =========>
Luuk
2018-06-03 16:20:54 UTC
Permalink
Post by G. B
Why is this program giving me this error when you run it?
Unhandled Exception: System.IndexOutOfRangeException: Index was
outside the bounds of the array.
   at Program.Main()
At what line number did you get this error?
Post by G. B
Joe,Blog,www.joeblog.com
Joe,Smith,www.joesmith.com
Sue,Blog,www.sueblog.com
<========= The program starts here =========>
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
class Program
{
    static void Main()
    {
        // // string[] lines = File.ReadAllLines (filePath);
        // List<string> lines = File.ReadAllLines (filePath).ToList();
        // foreach (string line in lines)
        // {
        //     Console.WriteLine( line  );
        // }
        // lines.Add("Sue,Storm,www.stormy.com");
        // File.WriteAllLines (filePath, lines);
        List<Person> people = new List<Person>();
        List<string> lines = File.ReadAllLines(filePath).ToList();
        foreach (var line in lines)
        {
            string[] entries = line.Split(',');
            Person newPerson = new Person();
            newPerson.FirstName = entries[0];
            newPerson.LastName = entries[1];
            newPerson.Url = entries[2];
            people.Add(newPerson);
        }
        Console.WriteLine("Read from text file\n");
        foreach (var person in people)
        {
{person.Url}");
        }
        people.Add(new Person { FirstName = "Greg", LastName =
"Jones", Url = "www.gregjones.com" });
        List<string> output = new List<string>();
        foreach (var person in people)
        {
output.Add($"{person.FirstName},{person.LastName},{person.Url}");
        }
        Console.WriteLine("Writing to text file ...\n");
        File.WriteAllLines(filePath, output);
        Console.WriteLine("All entries written ...\n");
        Console.ReadKey();
    }
    public class Person
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Url { get; set; }
    }
}
<========= The program ends here =========>
acc
2018-06-03 17:28:56 UTC
Permalink
        foreach (var line in lines)
        {
            string[] entries = line.Split(',');
            Person newPerson = new Person();
            newPerson.FirstName = entries[0];
            newPerson.LastName = entries[1];
            newPerson.Url = entries[2];
            people.Add(newPerson);
        }
foreach (var line in lines) {
string[] entries = line.Split(',');
if (entries.Length == 3) {
Person newPerson = new Person();
newPerson.FirstName = entries[0];
newPerson.LastName = entries[1];
newPerson.Url = entries[2];
people.Add(newPerson);
} else {
// wrong line format
}
}
Arne Vajhøj
2018-06-03 17:29:57 UTC
Permalink
Post by G. B
Why is this program giving me this error when you run it?
Unhandled Exception: System.IndexOutOfRangeException: Index was
outside the bounds of the array.
   at Program.Main()
Joe,Blog,www.joeblog.com
Joe,Smith,www.joesmith.com
Sue,Blog,www.sueblog.com
        List<Person> people = new List<Person>();
        List<string> lines = File.ReadAllLines(filePath).ToList();
        foreach (var line in lines)
        {
            string[] entries = line.Split(',');
            Person newPerson = new Person();
            newPerson.FirstName = entries[0];
            newPerson.LastName = entries[1];
            newPerson.Url = entries[2];
            people.Add(newPerson);
        }
The most obvious explanation is that there is a line in the file without
two commas.

Maybe a blank line.

Try:

string[] entries = line.Split(',');
if(entries.Length < 3)
{
throw new Exception("Houston we have a problem with line |" + line
+ "|");
}

Arne
G. B
2018-06-04 03:30:00 UTC
Permalink
Post by Arne Vajhøj
Maybe a blank line.
Yep it was the last line; IOW there was a Carriage Return after the
third line. Now everything works and I have incorporated the code given
by you and "acc". The revised program is:


using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


class Program
{
static void Main()
{
string filePath = @"c:\temp\test.dat";

List<Person> people = new List<Person>();

List<string> lines = File.ReadAllLines(filePath).ToList();

foreach (var line in lines)
{
string[] entries = line.Split(',');
if (entries.Length == 3)
{
Person newPerson = new Person();
newPerson.FirstName = entries[0];
newPerson.LastName = entries[1];
newPerson.Url = entries[2];
people.Add(newPerson);
}
else
{
// this one prints a problem line; A blank line prints
nothing!!
throw new Exception("There is a problem with one line
|" + line + "|");
}
}

Console.WriteLine("Read from text file\n");
foreach (var person in people)
{
Console.WriteLine($"{person.FirstName} {person.LastName}:
{person.Url}");
}

people.Add(new Person { FirstName = "Greg", LastName = "Jones",
Url = "www.gregjones.com" });

List<string> output = new List<string>();

foreach (var person in people)
{
output.Add($"{person.FirstName},{person.LastName},{person.Url}");
}

Console.WriteLine("Writing to text file ...\n");

File.WriteAllLines(filePath, output);

Console.WriteLine("All entries written ...\n");

Console.ReadKey();
}

public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Url { get; set; }
}
}

Continue reading on narkive:
Loading...