如果我写这段代码,那么为什么执行控制不会转到第二个while循环? [英] if i write this code then why the execution control is not going to the second while loop?
本文介绍了如果我写这段代码,那么为什么执行控制不会转到第二个while循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我编写此代码,那么为什么执行控制不会转到第二个while循环?以及为什么不读取文件首字母大写?
if i write this code then why the execution control is not going to the second while loop? and why the file capital is not being read?
#include<iostream.h>
#include<fstream.h>
#include<conio.h>
int main()
{
char ch[80];
ofstream of;
of.open("E:\\country",ios::out);
of<<"USA";
of<<"India";
of.close();
of.open("E:\\capital",ios::out);
of<<"Washington";
of<<"Delhi";
of.close();
ifstream fi;
fi.open("E:\\country",ios::in);
cout<<"The contents of country are :";
while(fi)
{
fi.getline(ch,50);
cout<<ch;
}
fi.close();
cout<<endl;
fi.open("E:\\capital",ios::in);
cout<<"The contents of capital are :";
while(fi)
{
fi.getline(ch,50);
cout<<ch;
}
fi.close();
getch();
return 0;
}
[edit]添加了代码块,HTML编码-OriginalGriff [/edit]
[edit]缺少分号-OriginalGriff [/edit]
[edit]Code block added, HTML encoded - OriginalGriff[/edit]
[edit]Missed a semicolon - OriginalGriff[/edit]
推荐答案
因为它永远不会离开您的第一个循环:
Because it never leaves your first loop:
while(fi)
{
...
}
永远不会退出,除非更改fi的值-到达文件末尾不会这样做.
更改两个循环以使用
will never exit unless the value of fi is changed - getting to the end of the file does not do that.
Change both your loops to use
while(fi.good())
{
...
}
这篇关于如果我写这段代码,那么为什么执行控制不会转到第二个while循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文