CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Apr 1999
    Posts
    22

    how to find a string from the file

    Let me explain my prgram here.
    I got a dialogbox here, and there are one edit box(for the user enter the file ), and two checkboxes(named "Top" and "Bottom"). The user gets the file from the directory, and after getting the file, the user need to click on one of box ("top" or"bottom"). Then the program will go through the file which the user enter in edit box, and find the right character ("T" or "B"). If m_Top ( the variable for checkbox"Top") is true, the program will pick only the data with the character"T", and put those data into a new file. If m_Bottom( the variable for checkbox "Bottom") is true, the program will pick only the data with the character "B", and put those data into another new file. My biggest problem is how to write some codes for going through the file the user enter in the edit box, and find the right character??? I will attach some of my codes here for reference.

    pik_in = fopen(text2, "r"); // finding file from directory and put into ED Box

    BOOL m_Top;
    BOOL m_Bottom;
    if (m_Top == TRUE) {
    while(!feof(pik_in)) {
    if (
    }
    }
    else if (m_Bottom == TRUE) {
    while (!feof(pik_in) {

    }
    }

    Anyone can help me out, I will very appreciated.

    al



  2. #2
    Join Date
    Apr 1999
    Posts
    90

    Re: how to find a string from the file

    void LocateChar(FILE *pik_in, TCHAR findChar)
    {
    if (pik_in == NULL) return;

    TCHAR buffer[256]; // fixed length buffer
    TCHAR seps[] = _T(" \t\n");
    TCHAR *token;

    // read an entire line from the file
    while (_fgetts(buffer, sizeof(buffer)/sizeof(buffer[0]), pik_in))
    {
    // tokenize the line into words
    token = _tcstok(buffer, seps);

    while (token != NULL)
    {
    if (_tcschr(token, findChar) != NULL)
    {
    TRACE("located char in word = '%s'\n", token);
    }

    // While there are tokens in "string"
    token = _tcstok(NULL, seps);
    }
    }
    }



  3. #3
    Join Date
    Apr 1999
    Posts
    22

    Re: how to find a string from the file

    Thank you for your help.
    I followed your code, and made some changes in my program. However, it still doesn't work because of some errors. Would you mind take a look my code, and help me out. I will attach my code here for you. Thank you for you help.

    pik_in = fopen(text2, "r"); // Open .pik file for reading Designators

    BOOL m_Top; // variable for check box OnTop
    BOOL m_Bottom; // variable for check box OnBottom
    TCHAR seps[] = _T(" \t\n");
    TCHAR *token;
    TCHAR findChar;

    if (m_Top == TRUE) {
    while (_fgetts(buf, sizeof(buf)/sizeof(buf[0]), pik_in))
    {
    // tokenize the line into words
    token = _tcstok(buf, seps);

    while (token != NULL)
    {
    if (_tcschr(token, findChar) != NULL)
    {
    TRACE("located char in word = '%s'\n", token);
    }

    // While there are tokens in "string"
    token = _tcstok(NULL, seps);
    }
    }
    }

    }
    else if (m_Bottom == TRUE) {
    while (_fgetts(buf, sizeof(buf)/sizeof(buf[0]), pik_in))
    {
    // tokenize the line into words
    token = _tcstok(buf, seps);

    while (token != NULL)
    {
    if (_tcschr(token, findChar) != NULL)
    {
    TRACE("located char in word = '%s'\n", token);
    }

    // While there are tokens in "string"
    token = _tcstok(NULL, seps);
    }
    }
    }
    }
    the compiler keep telling me thatbuf, pik_in, and m_Bottom are unerclarted. However, you can see that I already declared them. I don't know why it happens?


  4. #4
    Join Date
    Apr 1999
    Posts
    90

    Re: how to find a string from the file

    I don't see 'pik_in' or 'buf' declared.

    Try this and get back to me:

    FILE *pik_in = fopen(text2, "r");
    TCHAR buf[256]; // arbitrary size



  5. #5
    Join Date
    Apr 1999
    Posts
    90

    Re: how to find a string from the file

    Some additional changes are required:

    You need to initialize 'findChar' to the character that you are looking for.

    For example:

    if (m_Top)
    {
    findChar = (TCHAR)'T';
    }
    else
    {
    findChar = (TCHAR)'B';
    }

    while (_fgetts(buf, sizeof(buf)/sizeof(buf[0]), pik_in))
    {
    // tokenize the line into words
    token = _tcstok(buf, seps);

    while (token != NULL)
    {
    if (_tcschr(token, findChar) != NULL)
    {
    TRACE("located char in word = '%s'\n", token);
    }

    // While there are tokens in "string"
    token = _tcstok(NULL, seps);
    }
    }



  6. #6
    Join Date
    Apr 1999
    Posts
    22

    Re: how to find a string from the file

    Thank you for your time and help.
    Actually, it is only the first step of the program. Next thing, what I have to do is that If m_Top is true, the program will go through the file and find the character "T" (which you already help me out). Then, the program will pick only the data with the character"T", and put those data into a new file. If m_Bottom is true, the program will go through the file and find the character "B"(which you already help me out). the program will pick only the data with the character "B", and put those data into another new file.
    Do you have any good idea about it ? Please let me know.

    thank you again

    al



  7. #7
    Join Date
    Apr 1999
    Posts
    90

    Re: how to find a string from the file

    Give me your email address and I'll send you some code. This forum is too hard to write any code in.

    Michael


  8. #8
    Join Date
    Apr 1999
    Posts
    22

    Re: how to find a string from the file


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured