c++中的文件读取:for和while嵌套循环没有按预期工作:串行



我希望嵌套的for循环以串行顺序工作,例如考虑以下代码:

int main ()
{       
string mainstr;
ifstream infile;
ifstream infile1;
infile.open ("27032015.log");
infile1.open("Exordernumber");
for (unsigned int curLine = 0; getline(infile1,exordernum); curLine++)
{
    cout << "curLine:" << curLine << endl;
{
    for (unsigned int curLine1 = 0; getline(infile,mainstr);curLine1++)
        cout << "curLine1:" << curLine1 << endl;    
        if (mainstr.find(exordernum) != std::string::npos) 
                {
                    cout << "exordernum:"<<exordernum << ":" << endl;
                }                               
}
}

我希望它首先打印curLine:1
然后curLine1:{所有值}
然后curLine:2
然后curLine1:{所有值}
然后curLine:3
然后curLine1:{所有值}

等等。。

但它打印的东西:
curLine:1
然后curLine1:{所有值}
curLine:2
curLine:3
curLine:4

等等。。

错误在哪里
即使我使用while循环,同样的东西也会被打印出来
我想不通这个问题。

现在问题已经解决了,我想知道同样事情的python代码:这是我写的,它给出了一个错误。

#!/usr/bin/env python
import sys
import re
hand = open('Exordernumber')
for line1 in hand:
    with open('27032015.log') as f:
        for line in f:
            if re.search(line,line1):
                print line
        f.close()

请建议正确的python代码。

在每次外部迭代后,您必须将infile流重置为流的开头。我想你需要这样的

for (unsigned int curLine = 0; getline(infile1,exordernum); curLine++)
{
    cout << "curLine:" << curLine << endl;
    for (unsigned int curLine1 = 0; getline(infile,mainstr);curLine1++)
    {
        cout << "curLine1:" << curLine1 << endl;    
        if (mainstr.find(exordernum) != std::string::npos) 
                {
                    cout << "exordernum:"<<exordernum << ":" << endl;
                }                               
    }
    infile.seekg (0, infile.beg);
}

第一次到达文件"27032015.log"的末尾。第二次你永远不会去循环:

 for (unsigned int curLine1 = 0; getline(infile,mainstr);curLine1++)
        cout << "curLine1:" << curLine1 << endl;    

您必须将光标定位到文件的开头,或者在循环结束时重新打开它。

int main ()
{       
string mainstr;
ifstream infile;
ifstream infile1;
infile.open ("27032015.log");
infile1.open("Exordernumber");
for (unsigned int curLine = 0; getline(infile1,exordernum); curLine++)
{
    cout << "curLine:" << curLine << endl;
{
    for (unsigned int curLine1 = 0; getline(infile,mainstr);curLine1++)
        cout << "curLine1:" << curLine1 << endl;    
    if (mainstr.find(exordernum) != std::string::npos) 
    {
       cout << "exordernum:"<<exordernum << ":" << endl;
    }  
    infile.seekg (0, infile.beg);                    
}
}

http://www.cplusplus.com/reference/istream/istream/seekg/

内部循环只输入一次,因为在第一次迭代文件后,该循环的条件"getline(infile,mainstr)"永远不会得到满足。文件中的指针始终指向文件的末尾。

你可以做的是打开外循环中的文件:

for (unsigned int curLine = 0; getline(infile1,exordernum); curLine++)
{
    cout << "curLine:" << curLine << endl;
    infile.open ("27032015.log");
    {
        for (unsigned int curLine1 = 0; getline(infile,mainstr);curLine1++)
        cout << "curLine1:" << curLine1 << endl;    
        if (mainstr.find(exordernum) != std::string::npos) 
        {
            cout << "exordernum:"<<exordernum << ":" << endl;
        }                               
    }
    infile.close ();
}

因此,每次进入内部循环之前,输入文件都会被重新初始化。

对于python代码,您可以尝试删除最后一行"f.close()"。当您使用"with open"时,python将为您处理文件关闭。但您需要手动关闭文件"hand"。

#!/usr/bin/env python
import sys
import re
hand = open('Exordernumber')
for line1 in hand:
    with open('27032015.log') as f:
        for line in f:
            if re.search(line,line1):
                print line
hand.close()

你能发布python代码的错误消息吗?

相关内容

  • 没有找到相关文章

最新更新