Click here to Skip to main content
15,881,882 members
Home / Discussions / C#
   

C#

 
GeneralRe: How to search for a specific string in a file? Pin
StarBP6-Mar-10 11:28
StarBP6-Mar-10 11:28 
GeneralRe: How to search for a specific string in a file? Pin
SimpleData6-Mar-10 11:35
SimpleData6-Mar-10 11:35 
AnswerRe: How to search for a specific string in a file? [modified] Pin
harold aptroot6-Mar-10 11:32
harold aptroot6-Mar-10 11:32 
GeneralRe: How to search for a specific string in a file? Pin
SimpleData6-Mar-10 11:35
SimpleData6-Mar-10 11:35 
GeneralRe: How to search for a specific string in a file? Pin
harold aptroot6-Mar-10 11:37
harold aptroot6-Mar-10 11:37 
GeneralRe: How to search for a specific string in a file? Pin
Luc Pattyn6-Mar-10 12:11
sitebuilderLuc Pattyn6-Mar-10 12:11 
GeneralRe: How to search for a specific string in a file? [modified] Pin
harold aptroot6-Mar-10 12:21
harold aptroot6-Mar-10 12:21 
GeneralRe: How to search for a specific string in a file? Pin
SimpleData7-Mar-10 4:05
SimpleData7-Mar-10 4:05 
Yes, but I think that is not a problem for me. I am looking for a string in the file, no matter where it is.

I think code can express everything, in a better way. Here is my code:

private long DigBinary(string file, string strToDig)
        {
            FileStream fs = null;

            char[] chAim = strToDig.ToCharArray();
            char chTemp = '0';
            long latestHitBeginningLocation = 0;
            int locationInArray = 0;

            try { fs = new FileStream(file, FileMode.Open, FileAccess.Read); }
            catch { throw new Exception("An error occured while creating the stream."); }

            try
            {
                while (locationInArray < chAim.Length)
                {
                    chTemp = (char)fs.ReadByte();

                    if( chTemp != chAim[locationInArray] )
                        locationInArray = 0;

                    if (chTemp == chAim[locationInArray])
                    {
                        if (locationInArray == 0)
                            latestHitBeginningLocation = fs.Position - 1;

                        if (locationInArray == chAim.Length)
                            break;

                        locationInArray++;
                    }
                    else
                    {
                        locationInArray = 0;
                        latestHitBeginningLocation = 0;
                    }
                }
            }
            catch { throw new Exception("An error occured while reading the file."); }
            finally { if (fs != null) { fs.Close(); fs.Dispose(); } }

            return latestHitBeginningLocation;
        }


And yes, I know that my try-catch is useless. Big Grin | :-D
GeneralRe: How to search for a specific string in a file? Pin
Luc Pattyn7-Mar-10 8:41
sitebuilderLuc Pattyn7-Mar-10 8:41 
GeneralRe: How to search for a specific string in a file? Pin
SimpleData7-Mar-10 8:47
SimpleData7-Mar-10 8:47 
GeneralRe: How to search for a specific string in a file? Pin
Luc Pattyn7-Mar-10 9:09
sitebuilderLuc Pattyn7-Mar-10 9:09 
GeneralRe: How to search for a specific string in a file? Pin
SimpleData7-Mar-10 9:11
SimpleData7-Mar-10 9:11 
AnswerRe: How to search for a specific string in a file? Pin
Shane55557-Mar-10 3:28
Shane55557-Mar-10 3:28 
QuestionPlease help as i cant remember how to do this..... Pin
stephen.darling6-Mar-10 10:41
stephen.darling6-Mar-10 10:41 
AnswerRe: Please help as i cant remember how to do this..... Pin
StarBP6-Mar-10 11:05
StarBP6-Mar-10 11:05 
GeneralRe: Please help as i cant remember how to do this..... Pin
stephen.darling6-Mar-10 11:35
stephen.darling6-Mar-10 11:35 
AnswerRe: Please help as i cant remember how to do this..... Pin
Alan N6-Mar-10 11:35
Alan N6-Mar-10 11:35 
GeneralRe: Please help as i cant remember how to do this..... Pin
stephen.darling7-Mar-10 8:56
stephen.darling7-Mar-10 8:56 
QuestionGDI+ P/Invoke Pin
StarBP6-Mar-10 10:15
StarBP6-Mar-10 10:15 
AnswerRe: GDI+ P/Invoke [modified] Pin
DaveyM696-Mar-10 10:21
professionalDaveyM696-Mar-10 10:21 
GeneralRe: GDI+ P/Invoke Pin
StarBP6-Mar-10 10:36
StarBP6-Mar-10 10:36 
GeneralRe: GDI+ P/Invoke Pin
DaveyM696-Mar-10 10:40
professionalDaveyM696-Mar-10 10:40 
GeneralRe: GDI+ P/Invoke Pin
StarBP6-Mar-10 10:50
StarBP6-Mar-10 10:50 
QuestionCreating a web sniffer that will browse and get data from websites Pin
melquiades_is_alive6-Mar-10 8:23
melquiades_is_alive6-Mar-10 8:23 
AnswerRe: Creating a web sniffer that will browse and get data from websites Pin
Luc Pattyn6-Mar-10 8:56
sitebuilderLuc Pattyn6-Mar-10 8:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.