简体   繁体   English

在c ++中读取文件:for和while嵌套循环无法按预期工作:串行?

[英]file reading in c++: for and while nested loop are not working as expected: serially?

I expect the nested for loops to work in a serial order for example consider the following code: 我希望嵌套的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;
                }                               
}
}

I expect it to first print curLine: 1 我希望它首先打印curLine:1
then curLine1: {all the values} 然后curLine1:{所有值}
then curLine: 2 然后curLine:2
then curLine1: {all the values} 然后curLine1:{所有值}
then curLine: 3 然后curLine:3
then curLine1: {all the values} 然后curLine1:{所有值}

so on .. 等等..

but it prints the things: 但它打印出的东西:
curLine: 1 curLine:1
then curLine1: {all the values} 然后curLine1:{所有值}
curLine: 2 curLine:2
curLine: 3 curLine:3
curLine: 4 curLine:4

so on .. 等等..

Where is the mistake ? 错误在哪里?
Even if I use the while loops the same things gets printed. 即使我使用while循环,也会打印出相同的内容。
I can't figure out the problem. 我不知道问题所在。

Now as the problem has been solved, I would like to know the python code for the same thing: Here is what I have written it gives an error. 现在问题已解决,我想知道同一件事的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()

Kindly suggest the correct python code. 请提出正确的python代码。

first time it goes to the end of file "27032015.log". 第一次转到文件“ 27032015.log”的末尾。 Second time you will never go to the cycle: 第二次您将永远不会去循环:

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

you must position your cursor to the begin of file or reopen it in the end of loop. 您必须将光标置于文件的开头或在循环末尾重新打开它。

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/ http://www.cplusplus.com/reference/istream/istream/seekg/

You must reset infile stream to beginning of the stream after each outer iteration. 您必须在每次外部迭代后将文件内流重置为流的开头。 I think you need something like this 我想你需要这样的东西

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);
}

The inner loop is entered only once because the condition for that loop 'getline(infile, mainstr)' is never satisfied after the file is iterated for the first time. 进入内部循环仅一次,因为第一次循环访问该文件后,该循环“ getline(infile,mainstr)”的条件就不再满足。 The pointer in the file is always pointing to the end of file. 文件中的指针始终指向文件末尾。

What you could do is to open the file inside the outer loop: 您可以做的是在外部循环中打开文件:

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 ();
}

So the input file will be re-initialized everytime before entering the inner loop. 因此,输入文件将在进入内部循环之前每次都重新初始化。

For the python code, you can try remove the last line 'f.close()'. 对于python代码,您可以尝试删除最后一行'f.close()'。 When you use 'with open', python will handle file close for you. 当您使用“ with open”时,python将为您处理文件关闭。 But you need to close the file 'hand' manually. 但是您需要手动关闭文件“ 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()

Can you post the error message for the python code? 您可以发布python代码的错误消息吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM