我正在尝试从文件中读取数字

I am trying to read numbers from a file

本文关键字:文件 读取 数字      更新时间:2023-10-16
#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"未包含在此范围内,但我已经包含 cstudiostudio.h
我应该更改什么?

EOF

是一个函数,它是一个常量。但是,无论如何,您都不应该使用 eof 来查找文件的末尾(原因如下)。

将读取本身放入循环标头中,如下所示:

while(f >> a) {
    while(a>10)
      if(a%10!=0 && (a/10)%10!=0)  v[a%100]++;
    a/=10;
}

这样做的原因是返回 istreamf >> a 有一个转换运算符*,它允许将表达式用作条件。读取成功后,生成的条件计算结果为 true ;否则,它是false.

* 转换的详细信息在 C++98 和 C++11/14 中有所不同,但无论C++标准如何,表达式仍然有效。