Redian新闻
>
C++: How to read until the end of file?
avatar
C++: How to read until the end of file?# Programming - 葵花宝典
c*e
1
I have a txt file, which are 0 and 1 separated by space.
input.txt:
1 0 1 0 0 0 0 0
0 1 1 1 1 0 1
I would like to count the number of 0's and 1's. I use
the following code. My question is how to modify the while(?)
to make it read the txt file until the end? Thanks a lot.
ifstream fin;
fin.open("input.txt");
int i=0, count0=0, count1=0;

while(i==0 || i==1)
{
fin >> i;
if(i==0) count0++;
if(i==1) count1++;
}
avatar
o*r
2
while(fin>>i){
if( i == 0 ) count0++;
if( i == 1 ) count1++;
}

【在 c**********e 的大作中提到】
: I have a txt file, which are 0 and 1 separated by space.
: input.txt:
: 1 0 1 0 0 0 0 0
: 0 1 1 1 1 0 1
: I would like to count the number of 0's and 1's. I use
: the following code. My question is how to modify the while(?)
: to make it read the txt file until the end? Thanks a lot.
: ifstream fin;
: fin.open("input.txt");
: int i=0, count0=0, count1=0;

avatar
c*e
3
otter,
It works! Thank you very much.
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。