这可能是一个简单的问题,但我不明白我的代码有什么问题。我只是希望程序读取一个字符并将其从二进制文件返回给程序。我不明白为什么它不接受变量。这是一些代码:
int ncharf()
{
int neww;
myfile.get(neww);
return neww;
}
我检查了类似的问题,但他们没有帮助。我错过了什么吗?
这是错误:
严重级代码说明项目文件行抑制状态错误C2228左侧' .get'必须有class / struct / union
此外,它之前没有这样做,但现在它说myfile
是无效的标识符。
我能让它发挥作用的唯一方法就是取代" neww"使用" int",然后我无法返回值!
int main()
{
return 0; //To stop unwanted execution
char curchar[100000000];
ifstream myfile;
myfile.open("befen.bin", ios::in, ios::binary);
ofstream yourfile("enn.bin", ios::out);
int i = 1;
if (myfile.is_open())
{
int evar;
while (!myfile.eof())
{
int snchar[100000000];
snchar[i] = ncharf();
evar = rand() % 5 + 1;
if (evar = 1)
{
snchar[i] = (snchar[i] + 10);
}
if (evar = 2)
{
snchar[i] = (snchar[i] + 40);
}
if (evar = 3)
{
snchar[i] = (snchar[i] * 56);
}
if (evar = 4)
{
snchar[i] = (snchar[i] / 3);
}
yourfile << snchar[i];
yourfile << evar;
i = i + 1;
}
}
else
{
cout << "There was an error opening the file";
}
return 0;
}
我添加了主要功能。我如何合并你所说的@barmar?
答案 0 :(得分:1)
您忘记将myfile
作为参数传递。
int ncharf(istream &myfile)
{
char neww;
myfile.get(neww);
return (int)neww;
}
此外,正如@Barmar评论:The argument to .get() must be of type char, not int。
如果要从二进制文件中读取int,则应使用istream::read
代替:
int ncharf(istream &myfile)
{
int neww;
myfile.read((char*)&neww, sizeof(int));
return neww;
}