0

I'm doing some very basic coding in Visual Studio.

I am attempting to check the output of a command for a group of period separated digits or a string. It works fine when checking for a string but it doesn't seem to work if I ask it to look for a group of period separated digits such as 4.2.3

The code I am using is as follows

BOOL CheckLogForString(char* str)
{
    FILE* f = fopen (LogName, "r");
    if (NULL == f){ MessageBox(0, "Can't find log!","error",MB_APPLMODAL|MB_OK|MB_ICONSTOP);return FALSE;}
    BOOL found = false;
    while (!feof(f))
    {
        char buf[1000]="";
        if (fgets(buf,1000,f)==NULL) break;
        strupr(buf);
        if (0 != strstr (buf, str))found = TRUE;
    }
    fclose (f);
    return found;
}

I then call this as follows

if (CheckLogForString("WORDS"))
    {
        DO SOMETHING            
    }

That works fine but when I try

if (CheckLogForString("4.2.3"))
    {
        DO SOMETHING    
    }

It doesn't work. What can I do to make it work please?

  • Can you provide some extra information? Such as a few lines from an example Log file, showing the 4.2.3 you're looking for? – Yeraze Jun 21 '14 at 20:35
  • This code is unnecessarily C-like. `FILE *` should be replaced with `std::ifstream`. That allows you to safely remove the line to close it and kill off `found` and just return when you find it. The loop should be more like `for (std::string buf; std::getline(f, buf);)`. `strupr` can be done as a separate function with `std::transform` and the `strstr` can be replaced with `std::string::find`. Or you can forgo converting the string and do a case-insensitive comparison as described [here](http://stackoverflow.com/a/2886589/962089). – chris Jun 21 '14 at 20:40
  • Hi Chris, Thank you for your reply, I just found my error, it was totally unrelated to the code above. that code was given to me some time ago. I think it actually read a file called tmp.tmp but the file was some who write protected by the point that code was executed trying to look for the number. I added some code to kill the process that was holding on to the file and it began working immediately. Thank you anyway for your reply :) – CelticWebs Jun 21 '14 at 21:31

0 Answers0