简体   繁体   中英

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:

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
then curLine1: {all the values}
then curLine: 2
then curLine1: {all the values}
then curLine: 3
then curLine1: {all the values}

so on ..

but it prints the things:
curLine: 1
then curLine1: {all the values}
curLine: 2
curLine: 3
curLine: 4

so on ..

Where is the mistake ?
Even if I use the while loops the same things gets printed.
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.

#!/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.

first time it goes to the end of file "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/

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. 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()'. When you use 'with open', python will handle file close for you. But you need to close the file 'hand' manually.

#!/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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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