mardi 31 mars 2015

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.


Aucun commentaire:

Enregistrer un commentaire