Autor Beitrag
HoPPeL
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 51



BeitragVerfasst: Do 17.03.11 15:40 
hiho, möchte eine .dat auslesen und dessen Werte in ein ushort array packen. Habe das ganze jetzt versucht so zulösen:

ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
public void DataLoad(string Filename)
        {
            StreamReader sr = new StreamReader(Filename);
            ushort[] ImageData = {};
            int i = 1;

            while (! sr.EndOfStream)
            {
                ImageData[i] = ushort.Parse(sr.ReadLine());
                i++;
            }
            sr.Close();

            int j = 0;
            while (ImageData != null)
            {
                MessageBox.Show(ImageData[j].ToString() + "\n");
            }
        }


schaut m.M. eigentlich ganz gut aus, aber wenn ich die methode ausführen will bekomm ich immer "Der Index war außerhalb des Arraybereichs." als fehlermeldung... ist while (! sr.EndOfStream) nicht geeignet um zu gucken ob noch Zeilen vorhanden sind, oder was ist das Problem?

mfg
Ralf Jansen
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 4708
Erhaltene Danke: 991


VS2010 Pro, VS2012 Pro, VS2013 Pro, VS2015 Pro, Delphi 7 Pro
BeitragVerfasst: Do 17.03.11 15:49 
ausblenden C#-Quelltext
1:
ushort[] ImageData = {};					


Du hast ein Array der Länge 0 angelegt. Es gibt also nicht ein gültiges Element im Array somit wird jeder Zugriff auf eine IndexOutOfBoundsException laufen. Du must schon, wenn du ein Array benutzen willst, das in der richtigen Größe anlegen. Da du das vorraussichtlich vorher nicht weißt wäre eine List<ushort> wohl geeigneter. Übrigens ist ein Array 0 basiert und nicht 1 basiert. Dein Code legt den Index 0 nicht an(unter der Annahme dein Code würde funktionieren) aber nachher versuchst du trotzdem den auszugeben. Und das unendlich oft da ImageData zwar leer ist aber natürlich existiert und mehr prüfst du nicht in der 2.ten while Schleife.
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19339
Erhaltene Danke: 1752

W11 x64 (Chrome, Edge)
Delphi 12 Pro, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: Do 17.03.11 15:49 
user profile iconHoPPeL hat folgendes geschrieben Zum zitierten Posting springen:
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
public void DataLoad(string Filename)
        {
            StreamReader sr = new StreamReader(Filename);
            ushort[] ImageData = {};
            int i = 1;

            while (! sr.EndOfStream)
            {
                ImageData[i] = ushort.Parse(sr.ReadLine());
                i++;
            }
Du erzeugst ein leeres Array und weist dann den i-ten Eintrag zu.
HoPPeL Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 51



BeitragVerfasst: Do 17.03.11 16:04 
achso, ja alles klar... danke für die hilfe, habs jetzt einfach in eine Liste gepackt :)

mfg
thas11
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 25



BeitragVerfasst: Do 17.03.11 17:55 
Hi!

Ich habs zwar nicht ausprobiert, aber sollte die Variable i nicht mit 0 initialisiert werden ?
HoPPeL Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 51



BeitragVerfasst: Fr 18.03.11 12:42 
also nochmal eine frage:

immer wenn ich jetzt folgenden Code ausführe, bekomme ich ne OUTOFMEMORY meldung!

ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
StreamReader sr = new StreamReader(Filename);

            List<ushort> CamData = new List<ushort>();

            while (!sr.EndOfStream)
            {

                CamData.Add(ushort.Parse(sr.ReadLine()));
            }

            sr.Close();
Ralf Jansen
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 4708
Erhaltene Danke: 991


VS2010 Pro, VS2012 Pro, VS2013 Pro, VS2015 Pro, Delphi 7 Pro
BeitragVerfasst: Fr 18.03.11 13:08 
Wie groß ist den deine .Dat-Datei?
Eine generische Liste von ValueTypes sollte bei 2GB dicht machen.
HoPPeL Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 51



BeitragVerfasst: Fr 18.03.11 13:50 
habe extra zum testen ne kleine .dat angelegt mit folgenden werten

23264
22960
23280
23648
23712
23712
24144
24128
24912
25008

dennoch tritt der Fehler auf :(
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19339
Erhaltene Danke: 1752

W11 x64 (Chrome, Edge)
Delphi 12 Pro, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: Fr 18.03.11 14:20 
Wie wäre es, wenn du Peek statt EndOfStream benutzt wie im Beispiel in der Doku?
msdn.microsoft.com/d...reader.readline.aspx
HoPPeL Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 51



BeitragVerfasst: Fr 18.03.11 14:33 
ohhh :oops: ... das löst mein problem!
danke

mfg