在c++中读取文件:for和while嵌套循环未按预期工作:serially?

2024-04-24 20:24:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我希望嵌套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;
                }                               
}
}

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

以此类推。。你知道吗

但它会打印东西:
卷曲线:1
然后curLine1:{所有值}
卷曲线:2
卷曲线:3
卷发: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代码。你知道吗


Tags: 代码forstringlineopen曲线infileint
3条回答

在每次外部迭代之后,必须将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将为您处理文件关闭。但是你需要手动关闭这个文件。你知道吗

#!/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代码的错误消息吗?你知道吗

相关问题 更多 >