0

i am beginer and i don't know how to write from text file to list some strings. I mean i have a file :

A B
C D
E F
G H
...

and i want to write it to list but i dont know how, maybe it is simple but i tried something and it doesnt work. Now i have

List<List<string>> listaKolumn = new List<List<string>>();
var str = Application.GetResourceStream(new Uri("wzor.txt", UriKind.Relative));
StreamReader sreader = new StreamReader(str.Stream);

int x = 0;
while (!sreader.EndOfStream)
{
foreach (string k in sreader.ReadToEnd().Split(new char[] { ' ' }))
            {
                int j = 0;
                foreach (string l in k.Split(new char[] {' '}))
                {
                    if (listaKolumn.Count < k.Split(new char[] { ' '}).Length)
                    {
                        listaKolumn.Add(new List<string>());
                    }
                    //double temp2;
                    listaKolumn[j].Add(l);
                    j++;
                }
            }
}

but something is wrong. i know how it should be only in mind but i dontn know language very well and i can't write it.

IN SHORT i need this text wite in to multidimensional array like array[0][0] = A array[0][1]=B array[1][0] =C array[1][1] = D and so on

Karolina
  • 137
  • 3
  • 17

3 Answers3

3

Here is a method to pass in the text file you displayed in the question and return a multidimensional array.

public string[][] MultiArrayFromTextFile(string filePath)
{
    return File.ReadLines(filePath).Select(s => s.Split(' ')).ToArray();
}

File.ReadLines(filePath) returns a collection of all lines in the text file

.Select is an extension method that takes a function.

s=>s.Split(' ') is the function passed into .Select, which splits the string s by all spaces and returns an array of strings.

.ToArray() takes the collection of string arrays created by .Select and makes an array out of that, so you get array of arrays.

You can access the items in the results like this...

Console.WriteLine(results[1][1]); // returns 'D' ... so 2nd item of 2nd array
Jonathan Kittell
  • 7,163
  • 15
  • 50
  • 93
0

If I understand you right this should do the trick var listaKolumn = new List();

        var sreader = new StreamReader(@"f:\wzor.txt");

        string line;
        while ((line = sreader.ReadLine())!=null)
        {
            listaKolumn.Add(line);
        }

There doesn't seem to be any reason for you to have a list of lists (List>) - that would give you to lists.

If you need to split on whitespaces you can do that in the while-loop, like

foreach (var parts in line.Split(' '))
                {
                    listaKolumn.Add(parts);    
                }

Or by using LINQ

listaKolumn.AddRange(line.Split(' '));

Hope this helps...

woodbase
  • 169
  • 11
  • The point of this app is to count how many the same line is in two files. user displeys 2 files with text like at the top and program shoud count if at the file1 first line is the same as in file2 first line if yes then counter+1 and the same to with other lines in files – Karolina Nov 19 '14 at 13:52
  • 1
    What I would do is make two dictionaries. `var page1stuff = new Dictionary(), var page2stuff = new Dictionary()`. Int will be line number and string will be content. Then compare the two dictionaries. That way you can compare each line to any other line if needed or loop through them or whatever. – Jonathan Kittell Nov 19 '14 at 14:06
0

Okay - then I didn't understand it right. Found a smarter way to read the lines into a collection. This should get you pretty close at least :)

private static int LineCompare(string filepath1, string filepath2)
        {
            var result = 0;

            var list = File.ReadLines(filepath1).ToArray();
            var list2 = File.ReadLines(filepath2).ToArray();

            for (var i = 0; i < list.Length; i++)
            {
                if (list[i].Equals(list2[i]))
                    result++;
            }

            return result;
        }

Or the trimmed version:

private static int LineCompare(string filepath1, string filepath2)
        {
            var list = File.ReadLines(filepath1).ToArray();
            var list2 = File.ReadLines(filepath2).ToArray();

            return list.Where((t, i) => t.Equals(list2[i])).Count();
        }

This of course compares lines with the same index, so if you need to compare line 5 in file1 with line 2 in file2. You would need some kind of loop.

woodbase
  • 169
  • 11
  • thats what i need at this moment, thanks, but in progress with this app i would be needed this collection, or multidimensional array, because i must do different operations on this strings like if there is in some lines "-" character than do something, so i wanted write this strings or chars in arrays to be able to do in the future some operations and compare two arrays to each other and then displays result to user on screen – Karolina Nov 19 '14 at 14:38
  • so in short ai need this text to write to multidimensional array, can you show me how? – Karolina Nov 19 '14 at 14:40
  • i can and i did it in c# but in desktop application but in windows phone app are harder to me because i have never wrote any windows phone app – Karolina Nov 19 '14 at 14:43