计算TXT文件中元素的数量
Count the number of elements in txt file
我有这样的作业:
/*
- Read an input text file (.txt) contain one line to store an array of integer:
Input.txt
4 1 2 -100 -3 10 98 7
- Write SumList function to sum all integer data of the list
- Write a function to find the max of all integer data
- ...
*/
我的问题是如何计算txt文件中的数量数量/for(int i = 0; i< n; i (,n是文件/用于读取文件的数字。还是在不初始化n的情况下读取此文件的其他方法?谢谢!
您的真正问题是:如何通过Word读取文件。
我相信您已经知道什么是文件流,所以这是代码:
fstream file("yourfile.txt", ios::in);
std::string word;
while (file >> word)
{
// convert word to int
}
现在下一个问题是:如何将字符串转换为int。我希望您可以自己弄清楚---- http://www.cplusplus.com/reference/cstdlib/atoi/
另外,这将更容易:(感谢@fei Xiang(
int i;
while (file >> i)
{
// do something
}
相关文章:
- Mongodb c++驱动程序:如何查询元素的数组
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 使用strcpy将char数组的元素复制到另一个数组
- 使用不带参数的函数访问结构元素
- 给定n个元素的m个集合.在C++中找到出现在最大集合数中的元素
- C++如何通过用户输入删除列表元素
- lower_bound()返回最后一个元素
- 如何将 txt 文件中的行分隔为组件C++
- 基于多个条件处理地图中的所有元素
- 调整大小后指向元素值的指针unordered_map有效?
- 使用std::transform将一个范围的元素添加到另一个范围中
- 使用函数"remove"删除重复元素
- 具有最大子序列大小的序列,每个元素都相同
- 如何将两个不同矢量的同一位置的两个元素组合在一起
- 如何将元素添加到数组的线程安全函数?
- 计算TXT文件中元素的数量
- c++ 计算 txt 文件中每对元素的出现次数
- c++按元素编辑txt文件
- 如何从填充.txt文件名的向量中选取随机元素C++
- 矢量元素和 txt 文件行之间的字符串比较不起作用