Re: Input file *.txt to dialog box

Tech-Archive recommends: Fix windows errors by optimizing your registry

From: Jase (jshelley_at_nospam.optushome.com.au)
Date: 03/30/04


Date: Tue, 30 Mar 2004 19:32:53 +1000

Here's how you search for a complete string. Firstly, store the string you
are searching for in a char array. You will also need a pointer to the
current character in the string. Then, you loop through the stream,
searching for each character in turn, incrementing the current character
pointer each time. Here's a bit of code that will be much easier to
understand ;-) Using the MFC class CFile rather than stdio functions.

char szMatch[] = "[FoundSection]\r\n";
char * cur = szMatch; // cur now points to the first character
char ch;
CString strOut;

// assumes the CFile object f has been opened with read privileges
CArchive ar(&f, CArchive::load); // use CArchive as it provides buffering
for faster single char access
while (ar.Read(&ch, 1)) {
    if (cur == szMatch) { // we're not in a match
        if (ch == *cur)
            cur++;
    } else {
        if (ch == *cur) {
            cur++;
            if (*cur == '\0') {
                // complete string match
                break;
            }
        } else
            cur = szMatch; // incomplete match. Reset.
    }
}
if (*cur == '\0') { // string matched
    while (ar.Read(&ch, 1)) {
        if (ch == '\r') // if this is the first of a /r/n, which are two
characters, btw
            break;
        strOut += ch;
    }
    m_edit.SetWindowText(strOut); // m_edit is a CEdit variable created
in the class wizard
} else {
    // section not found
}

Jase

"Ray Schmidt" <raymondschmidt@bellsouth.net> wrote in message
news:173F7624-3FB4-4381-BE70-134E21DDB70B@microsoft.com...
> Because I'm ignorant and not sure how to test for then entire string to
match that condition. I keep trying to sequentally read down each char:
>
> for( i = 0; (i < 80) && ((ch = getchar()) != EOF)
> && (ch = '\r\n'); i++ )
> {
>
> buffer[i] = (char)ch;
> ch = fgetc( *stream );
> }
>
> Please, I'm sure this looks like silly mistake, however, I'm trying...
>
>
>
>



Relevant Pages

  • RE: Filtering Data On Entry
    ... So if you are searching for a Surname and the name of the control (not the ... Private Sub ComboName_NotInList(NewData As String, ... Dim rs As DAO.Recordset ... character in the field when creating a new record. ...
    (microsoft.public.access.gettingstarted)
  • Re: Finding substring in character array
    ... It works with a character array instead. ... is that there's no way to make one without already having all the characters in an array which then gets copied into the string. ... whereas you could fill all 1 MB with a char[]. ... If he insists on searching a char, i would suggest he reads up on the Boyer-Moore string searching algorithm. ...
    (comp.lang.java.programmer)
  • RE: Filtering Data On Entry
    ... If you are adding or searching for a record that it not already ... Private Sub ComboName_NotInList(NewData As String, ... Dim rs As DAO.Recordset ... character in the field when creating a new record. ...
    (microsoft.public.access.gettingstarted)
  • Re: finding strings in a text file help
    ... digits and reserved words and then prints them out in order ... > it gets the whole string matches it against the reserved words array ... one character of a potential word in your "s" string. ... a char[] array would do. ...
    (comp.lang.java.help)
  • Re: remove spaces from a string and Complexity
    ... string character by character and copying onto another output string. ... void delchar(char *s, char c) ... I've seen functions written as above, however I'm still a little confused about one point - C passes by value therefore with your above function wouldn't the following behave incorrectly (incorrectly as in not modify the contents referenced by the first parameter but instead modify a copy of it): ...
    (comp.lang.c)