Click to See Complete Forum and Search --> : how to find a string from the file


yang
April 22nd, 1999, 09:38 AM
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

Michael Decker
April 22nd, 1999, 10:13 AM
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);
}
}
}

yang
April 22nd, 1999, 12:26 PM
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?

Michael Decker
April 22nd, 1999, 12:35 PM
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

Michael Decker
April 22nd, 1999, 12:42 PM
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);
}
}

yang
April 22nd, 1999, 01:00 PM
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

Michael Decker
April 22nd, 1999, 01:36 PM
Give me your email address and I'll send you some code. This forum is too hard to write any code in.

Michael

yang
April 22nd, 1999, 01:50 PM
azy@powermonitors.com