c**********e 发帖数: 2007 | 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++;
} | o***r 发帖数: 81 | 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;
| c**********e 发帖数: 2007 | 3 otter,
It works! Thank you very much. |
|