Hi

Suppose you want to read from a file in a loop with strings of various lengths into a single string variable e.g if your file contents are such as

Steve West , 14/10/1985, Indiana, FARh+ , 01234567890, Sht. Mehmet Emin Street No:1 Gocmenkoy Nicosia KKTC

Marcus Chaplin, 13/11/1978 ,Colorado , MBRh+ , 01234567890, Kelebek Street No:11 Taskinkoy Nicosia KKTC

Here's what i've tried. Seems correct but I'm getting an exception "Array inces is out of bounds" on the line "date = words[1]" during the second loop. How can i fix this? Thanks

while (true)

            {

                info = myReader.ReadLine();

                if (info == null) break;

                string[] words = info.Split(',');

                name  = words[0];

                date  = words[1];

                place = words[2];

                blood = words[3];

                num   = words[4];

                address = words[5];
}

It means you have a line that doesn't have a ',' character in it. Most likely it is the last line of the file. Try checking for an empty line before you split it.

info = myReader.ReadLine();
if (info == null) break;
if (info.Trim() != String.Empty) {
    string[] words = info.Split(',');
    if (words.Length == 6) {
        name = words[0].Trim();
        date = words[1].Trim();
        ... etc. ...
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.