我正在尝试从文件中读取数字
I am trying to read numbers from a file
#include <iostream>
#include <fstream>
#include <cstdio>
#include <stdio.h>
using namespace std;
int v[101];
int main()
{
int max=0; int a,i;
ifstream f("bac.in");
ofstream g("bac.out");
while(!EOF(f))
{
f >> a;
while(a>10)
if(a%10!=0 && (a/10)%10!=0) v[a%100]++;
a/=10;
}
for(i=10;i<=99;i++) if(v[i]>max) max=v[i];
for(i=10;i<=99;i++) if(v[i]==max) g<<i;
}
我收到错误 14 | 错误:"-1"不能用作函数
如果我使用 eof 而不是 EOF,我会得到错误"eof"未包含在此范围内,但我已经包含 cstudio 或 studio.h
我应该更改什么?
EOF
不是一个函数,它是一个常量。但是,无论如何,您都不应该使用 eof
来查找文件的末尾(原因如下)。
将读取本身放入循环标头中,如下所示:
while(f >> a) {
while(a>10)
if(a%10!=0 && (a/10)%10!=0) v[a%100]++;
a/=10;
}
这样做的原因是返回 istream
的 f >> a
有一个转换运算符*,它允许将表达式用作条件。读取成功后,生成的条件计算结果为 true
;否则,它是false
.
* 转换的详细信息在 C++98 和 C++11/14 中有所不同,但无论C++标准如何,表达式仍然有效。
相关文章:
- 正在将csv文件读取为双精度矢量
- 将从格式化文本文件读取的文本数据存储到链表
- C++ 读取文件读取文件不正确
- 使用对象文件读取三角形数据网格
- 如何减慢从 BVH 文件读取的 opengl 动画?
- 从文件读取,并写入函数C++中的数组
- 从对象C++中的文件读取内容时出现分段错误
- 从文件读取并通过矢量输出到控制台
- 尝试将字符串从文件读取到无符号字符向量中
- 如何将文本文件读取到二维数组中并以 c++ 打印
- 简化文件读取的函数
- 如何将逗号分隔的文件读取为 2D int 数组?
- 在 c++ 中将数据从文件读取到队列中
- 'std::wstring_convert'尽可能多地转换(从 UTF8 文件读取块)
- C++,从文件读取到结构,然后读取到向量(结构被推入向量太多次,而不仅仅是一次)
- 文件读取C++包括空格键
- 从文件读取时未将值推回矢量
- 从文件 C++ 读取时的无限循环
- 从同一文件读取和写入?
- 从.txt文件读取到C++中的双精度向量