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++;
}
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++;
}