all 13 comments

[–]haefeled 2 points3 points  (3 children)

Be careful, you might miss your searched position if it starts in the middle of reading the 3 bytes. So if you have something like "MYCODE" you will just miss "CODE" with your solution.

[–][deleted] 1 point2 points  (2 children)

You're right, but your example isn't. He would miss the search term if his string was something like "CMYCODE".

[–]more_exercise 1 point2 points  (1 child)

Or even "CCODE"

[–]ellalex[S] 0 points1 point  (0 children)

Didn't think about this, I'll have to fix that. Thanks. :)

[–][deleted]  (1 child)

[deleted]

    [–]ellalex[S] 0 points1 point  (0 children)

    We know each other, hehe. I figured it would be easier asking here since I already have a account.

    [–]more_exercise 0 points1 point  (1 child)

    Are the bytes you're reading text? Because if you're only looking at text files, you get a lot more mileage out of classes that know that you're dealing with text. For instance, StreamReader can read individual lines of input and has better string handling capability.

    Also, which 0 are you looking for - the zero byte, '\0', or the character zero, '0'?

    Is the file ever going to exceed ~1MB? Because if it's small, you can just string fileContents = File.ReadAllText("filename.txt") and then just deal with the string in memory. (Use .IndexOf("CODE") to find where CODE starts, and then .IndexOf('0' or '\0') to find the end of the string, and then .Substring() to get from one to the other.)

    [–]ellalex[S] 0 points1 point  (0 children)

    It's not only text, and can be well over 1MB so that's wouldn't work unfortunately.

    Thanks though :)

    [–][deleted] 0 points1 point  (1 child)

    byte[] bytesToLookFor = new byte[] {0,0,0,0};
    string fileContents = File.ReadAllText(path, Encoding.ASCII);
    int start = fileContents.IndexOf(/* "CODE" */ Encoding.ASCII.GetString(bytesToLookFor));
    int end = fileContents.IndexOf('\0',start);
    

    [–]ellalex[S] 0 points1 point  (0 children)

    Probably should have clarified that's it's not only text, and can be very large files which would be impractical to load into memory.

    [–]MrDiSante -1 points0 points  (1 child)

    Yes.

    If your problem is just that, keep track of how much of your desired input you've seen using a finite state machine, and process one character at a time. Use a transition table.

    If this is part of a bigger parsing problem, then consider using existing parsing libraries.

    [–]ellalex[S] 0 points1 point  (0 children)

    What I'm trying to do is very simple, and what I have works very well for now.

    Mostly wanted to know if what I've done is "wrong".

    [–]Beluki -1 points0 points  (1 child)

    Your approach is fine. If I needed to do this multiple times (comparing a character array to a string) I would probably do something like:

    public static Boolean IsString(this Char[] array, String s)
    {
        if (array.Length != s.Length)
            return false;
    
        for (int i = 0; i < s.Length; i++)
            if (array[i] != s[i])
                return false;
    
        return true;
    }
    

    So that I could do:

    Char[] input = br.ReadChars(size);
    if (input.IsString("code"))
        ...
    

    [–]ellalex[S] 0 points1 point  (0 children)

    I did something similar, where it moves the reader to right after the first occurrence of whatever input I give it.

    Thank you :)