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

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' was not included in this scope 但我已经包含 cstudiostudio.h
我应该改变什么?

EOF 不是一个函数,它是一个常量。但是,您不应该使用 eof 来查找文件结尾 (here is why)。

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

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

这样做的原因是 f >> a,即 returns istream,有一个转换运算符 *,它允许表达式是作为条件使用。当读取成功时,结果条件的计算结果为 true;否则,它是 false.

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