由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - C++: How to read until the end of file?
相关主题
关于文件读取的C++ 问题?how to read a sentence into a vector of string?
有人能解释一下这段C++代码吗C++ Q 99-102 (转载)
How to read binary(data) file generated by Fortran in C/C++ (转载)请教一个C++的问题
C++文件读取数值问题C++中如何数据文件一起build进exe文件中?
这个面试题有什么trick?Urgent question
How to read a simple csv file in C++?C++ 在 windows 上 结果正确, 在 linux 上结果总是不一样,怎
C++读文本文件怎么判断换行?急!java 入门, 代码问题~~请各位好心人帮帮忙!
问一个C++文件读取的问题How to see the content of a library file
相关话题的讨论汇总
话题: count0话题: count1话题: fin话题: file话题: c++
进入Programming版参与讨论
1 (共1页)
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.
1 (共1页)
进入Programming版参与讨论
相关主题
How to see the content of a library file这个面试题有什么trick?
C++ string类输入数据的问题How to read a simple csv file in C++?
what's wrong with this C++ code?C++读文本文件怎么判断换行?
C++: define a reference always reference the same object问一个C++文件读取的问题
关于文件读取的C++ 问题?how to read a sentence into a vector of string?
有人能解释一下这段C++代码吗C++ Q 99-102 (转载)
How to read binary(data) file generated by Fortran in C/C++ (转载)请教一个C++的问题
C++文件读取数值问题C++中如何数据文件一起build进exe文件中?
相关话题的讨论汇总
话题: count0话题: count1话题: fin话题: file话题: c++