我希望嵌套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代码。你知道吗
在每次外部迭代之后,必须将infile流重置为流的开头。我想你需要这样的东西
第一次转到文件“27032015.log”的末尾。 第二次你再也不会骑自行车了:
必须将光标定位到文件的开头或在循环结束时重新打开它。你知道吗
http://www.cplusplus.com/reference/istream/istream/seekg/
只进入一次内部循环,因为在第一次迭代文件之后,循环“getline(infile,mainstr)”的条件永远不会满足。文件中的指针始终指向文件的结尾。你知道吗
您可以在外循环中打开文件:
因此输入文件在每次进入内部循环之前都会被重新初始化。你知道吗
对于python代码,可以尝试删除最后一行“f.close()”。当您使用'with open'时,python将为您处理文件关闭。但是你需要手动关闭这个文件。你知道吗
你能发布python代码的错误消息吗?你知道吗
相关问题 更多 >
编程相关推荐