CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1

    Problem Finding a string in a binary file

    I have a program that opens a file in binary mode and searches through it to find a string. It works sometimes, but on longer files it just
    doesn't seem to read all the data into currentLine. The file that I am reading is a .doc that is about 3 pages of text, and I went
    through the debugger and it seemed not to read the first 100 or so charaters in the text part of the file, and the code can't be specific for any
    one kind of file, so just reading the .doc header as a case would be out of the question. Any help woould be greatly appreciated. Thanks.


    ...
    // search string is a value read from a file earlier
    CStdioFile file;
    bool fileIsClosed;
    fileHasMoved = false;
    if(!file.Open(currentFile.GetFilePath(),CFile::modeRead|CFile::typeBinary))
    {// begin error mesage
    MessageBox(currentFile.GetFilePath() + " was failed to be opened. Do you have it open if so please close it and archive again.");
    fileDest.Close();
    currentFile.Close();
    return false;
    }// end error message
    fileIsClosed = false; // file is open
    searchString.MakeLower();
    CString currentLine;
    while(file.ReadString(currentLine) != 0)
    { // begin Get file line
    currentLine.MakeLower();
    if(currentLine.Find(searchString) != -1)
    { // begin found string in current line
    // do stuff when searchString is found in file
    } // end found string in current line
    } // end get file line
    ...




    Anish Mistry
    "The seeds of the future lie buried in the past."
    --The Oracle
    http://am-productions.8m.com/
    [email protected]

  2. #2
    Join Date
    Jun 1999
    Location
    Connecticut, USA
    Posts
    174

    Re: Problem Finding a string in a binary file

    The problem is your looking at a binary file data, but using a string to read everything. The minute you hit a NULL value in the string, the search exits (binary files can contain the value 0). You will have to read into a buffer, then search the buffer for the subset. That way, you can ignore the NULL values that are present.


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