Autor Beitrag
felix96
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 34


VS 2010 Express
BeitragVerfasst: Sa 24.09.11 18:49 
Hallo,

ich verzweifle jetzt schon seit Sunden (naja, nicht ganz :-) ) an folgendem Problem:

Ich habe eine Datei, die ich in ein Array bekommen will.
Das ganze soll folgendermaßen aussehen:

Datei:
Zeile1
Zeile2
Zeile3

Array:
1 => Zeile1
2 => Zeile2
3 => Zeile3

(x => ZeileX entspricht array[x])

Hier mal mein aktueller ansatz:
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
private ArrayList ReadFile(string path)
        {
            ArrayList dataList = new ArrayList();
            if (File.Exists(path))
            {
                StreamReader sr = new StreamReader(path);
                string data = sr.ReadToEnd(); sr.Close(); if
                    (!string.IsNullOrEmpty(data))
                {
                    dataList.AddRange(data.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries));
                }
            }
            return dataList;
        }

// ...

arr = (string[]) ReadFile("datei.txt").ToArray(typeof(string));


Wobei hier arr keine Werte enthält :-(

Danke für's lesen

Moderiert von user profile iconChristian S.: Highlight- durch C#-Tags ersetzt
Christian S.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 20451
Erhaltene Danke: 2264

Win 10
C# (VS 2019)
BeitragVerfasst: Sa 24.09.11 18:54 
Also, erstens ist ArrayList in den allermeisten Fällen nicht zu empfehlen, weil es nicht typensicher ist. Benutze entweder ein richtiges Array, sofern die Länge vorher bekannt ist, oder ansonsten einen List<T>.

Und ansonsten würde ich einfach die ReadAllLines-Methode der File-Klasse benutzen ;-)

_________________
Zwei Worte werden Dir im Leben viele Türen öffnen - "ziehen" und "drücken".
C#
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 561
Erhaltene Danke: 65

Windows 10, Kubuntu, Android
Visual Studio 2017, C#, C++/CLI, C++/CX, C++, F#, R, Python
BeitragVerfasst: Sa 08.10.11 22:38 
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
private List<string> ReadFile(string path)
        {
            List<string> dataList = new List<string>();
            if (!File.Exists(path)) return null;
            string data = File.ReadAllLines(path);
            foreach(string s in data.Split('\n''\r')) dataList.Add(s);                 
            return dataList;
        }

// ...

//Entweder
List<string> arr = ReadFile("datei.txt");
//Oder
string[] arr = ReadFile("datei.txt").ToArray();


So müsste es doch eig klappen.

_________________
Der längste Typ-Name im .NET-Framework ist: ListViewVirtualItemsSelectionRangeChangedEventHandler
Christian S.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 20451
Erhaltene Danke: 2264

Win 10
C# (VS 2019)
BeitragVerfasst: Sa 08.10.11 23:11 
ReadAllLines liefert doch schon ein String-Array, Deine Methode wird also weder funktionieren noch ist sie notwendig.

_________________
Zwei Worte werden Dir im Leben viele Türen öffnen - "ziehen" und "drücken".
C#
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 561
Erhaltene Danke: 65

Windows 10, Kubuntu, Android
Visual Studio 2017, C#, C++/CLI, C++/CX, C++, F#, R, Python
BeitragVerfasst: Sa 08.10.11 23:48 
Hoppla stimmt ja xD. Ich war bei ReadAllText :oops: mein Fehler.

_________________
Der längste Typ-Name im .NET-Framework ist: ListViewVirtualItemsSelectionRangeChangedEventHandler