【问题标题】:C++ How to determine and report offset for current position in binary file using pubseekoff?C ++如何使用pubseekoff确定和报告二进制文件中当前位置的偏移量?
【发布时间】:2013-04-26 23:17:38
【问题描述】:

我只使用 C++ 大约 6 个月,因此对于我在代码中犯的任何愚蠢错误,我深表歉意。

我正在做一个项目,该项目将在二进制文件中读取到 streambuf,然后在 streambuf 中搜索特定序列,并报告二进制文件中的序列起始偏移量以及在二进制文件。

要搜索的序列存储在一个称为 snortRules 的向量中,其中 snortRules 向量的每个索引都是存储为整数向量的单个序列。

最终我需要创建一个文本文件,该文件将显示找到的偏移量和特定序列,以便与另一个应该匹配的文件进行比较,但现在我只是将结果打印到控制台窗口。

我的代码似乎可以正常工作,因为它报告发现除了一个问题之外的所有序列。出于某种原因,我有时会重复找到序列的偏移量。例如输出将是:

101521 #655
101816 #656    <--This output is correct
101816 #657    <--This output is wrong and should have offset 101865 
101893 #658    <--This output is correct
101893 #659    <--This output is wrong and should have offset 102084
102105 #660
102325 #661
102378 #662

我认为我的问题是我没有使用 streambuf 的 pubseekoff 正确获得偏移量。

我的代码是:

fstream binFS(binName, ios::in | ios::binary); //create stream 
streambuf* binBuff = binFS.rdbuf(); //create streambuf from filestream
int currentChar; //ascii code of current character from binary file
streamsize offset; //location of first character of rule in binary file
do
{ //until the end of binary file
    for(unsigned i=0; i<snortRules.size(); ++i)
    {//start by finding the begining of current rule    
        do
        { //check binBuff until first char of first rule is found.
            currentChar = binBuff->sgetc();
            if( snortRules[i][0] == currentChar )//binBuff->sgetc() ) //compare short rule 1st char to curtent position 
            { //match save offset and compare rest of rule
                offset = binBuff->pubseekoff(0,ios::cur); //set the offset to current postion
                bool checkNext = true; //bool to break loop when checking rules if not matching
                for(unsigned srIdx=0; srIdx < snortRules[i].size() && checkNext == true; ++srIdx)
                { //loop through current rule comparing characters                        
                    if(snortRules[i][srIdx] == currentChar)
                    { //match check if end of rule or advance character to compare to next in rule
                        if( srIdx == snortRules[i].size()-1)
                        { //match write the offset of fist character of rule and which was matched
                            std::cout<<offset<<" #"<<i+1<<endl; //write to console for debugging purposes will write to text file when working
                            ++i; //increment rule to next when exact match found
                        }
                        else
                        { //not at the end of the rule so continue to compare next characters
                            currentChar = binBuff->snextc(); //set currentChar to next char to be checked by for loop
                        }
                    }
                    else
                    { //no match break out and continue 
                        checkNext == false; //set flag to break for loop because characters didnt match
                    }
                }
            }
            else
            { //no match check next character in binBuff
                //offset = binBuff->pubseekoff (0,binFS.cur, binFS.in); //set the offset to current postion
                currentChar = binBuff->snextc(); //get next char in binBuff
                //offset = binBuff->pubseekoff (0,binFS.cur, binFS.in); //set the offset to current postion
            }
        }while( currentChar != streambuf::traits_type::eof() ); //stop checking if end of file reached
    }
}while( binBuff->snextc() != streambuf::traits_type::eof() ); //stop checking if end of file reached

我曾尝试在 stackoverflow.com 和 cplusplus.com 上查找 pubseekoff 的文档和示例,但我能找到的内容对我来说没有多大意义,老实说,我不是 100% 确定这是我的问题...

非常感谢任何帮助或建议!

【问题讨论】:

  • 不幸的是,我是新手,8 小时内无法回答自己的问题。想要一个简单的答案?关闭这个问题,否则我会在早上检查。整个问题在于: checkNext == false; "==" 没有任何效果,但杀死了逻辑,因为它需要: checkNext = false;哪个分配布尔值并正确终止循环。我忽略了编译器在 VS 中发出的警告并追了我几个小时不是傻瓜......如果你有没有任何意义的随机运行时错误:检查警告!祝你好运!

标签: file visual-c++ binary streambuf


【解决方案1】:

整个问题是一行:

checkNext == false;

“==”无效并导致错误,因为它需要:

checkNext = false;

这样它就会分配布尔值并在适当的时间终止循环。

我忽略了Visual Studio中编译器发出的警告,追了几个小时,现在我觉得自己像个傻瓜。

因此,如果您需要相关的 streambuf 示例,这可能会有所帮助。如果您遇到一些没有任何意义的随机运行时错误,请检查警告。

【讨论】:

    猜你喜欢
    • 2013-08-06
    • 2015-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-17
    • 2021-01-04
    • 1970-01-01
    相关资源
    最近更新 更多