Autor Beitrag
AeroX
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 68



BeitragVerfasst: Mo 14.04.08 00:01 
tach,
ich will eine Datei Byte-weise auslesen,
aber ich kriege immer EndOfStreamException.

ausblenden volle Höhe C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace SpeedSearch
{
    class Program
    {
        char[] Chars;
        byte[] Bytes;
        byte b;
        string str = "das";
        int i;

        static void Main(string[] args)
        {
            Program P = new Program();
            P.Start();
        }

        private void Start()
        {
            Chars = str.ToCharArray();
            Bytes = new byte[Chars.Length];
            for (i = 0; i < Chars.Length; i++)
            {
                Bytes[i] = Convert.ToByte(Chars[i]);
            }
            FileStream FS = new FileStream("test.txt",FileMode.Open);
            BinaryReader BR = new BinaryReader(FS);
            while ( (b = BR.ReadByte()) != 0)            // hier is das problem
            {
                if (Bytes[0].Equals(b))
                {
                    Console.WriteLine("Gefunden!");
                }
            }
            Console.Read();
        }
    }
}
JüTho
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2021
Erhaltene Danke: 6

Win XP Prof
C# 2.0 (#D für NET 2.0, dazu Firebird); früher Delphi 5 und Delphi 2005 Pro
BeitragVerfasst: Mo 14.04.08 09:46 
Hallo,

nach Lektüre der SDK-Doku habe ich die Vermutung, dass BinaryReader nur beschränkt geeignet ist, weil die Dateiposition dabei nicht abgefragt werden kann. Du musst wohl wirklich try-catch benutzen oder mit PeekChar und dem einfachen Read arbeiten.

Jürgen
Th69
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Moderator
Beiträge: 4799
Erhaltene Danke: 1059

Win10
C#, C++ (VS 2017/19/22)
BeitragVerfasst: Mo 14.04.08 09:48 
Dann fang doch einfach diese Ausnahme auf:
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
try
{
   ...
}
// Catch the EndOfStreamException
catch(EndOfStreamException e)
{
}
AeroX Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 68



BeitragVerfasst: Mo 14.04.08 12:38 
Danke für die schnelle Antwort.
Kha
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 3803
Erhaltene Danke: 176

Arch Linux
Python, C, C++ (vim)
BeitragVerfasst: Mo 14.04.08 17:54 
Ouch! So langsam will ich dir nicht mehr ein Anfängertutorial empfehlen, sondern es lieber ärztlich verschreiben :? . Du kannst einfach nicht ewig weiter drauf los programmieren, ohne lokale Variablen zu kennen.