大侠帮忙看看C++的错误啊,谢谢了

来源:百度知道 编辑:UC知道 时间:2024/07/07 04:41:48
using namespace std;
#include <fstream>
#include <iostream>
#include <cstring>

char *func(ifstream file,char *str)
{
char line[256];
while(!file.eof())
{
file.getline(line,256);
strcpy(str,line);
}
return str;
}

int main()
{
ifstream file("example.txt");
if(!file.is_open())
{
cout<<"error\n";
}
else
{
char *str1=new char[20];
char *str=new char[20];
str1=func(file,str);
cout<<str1;
delete str;
delete str1;
}
getchar();
return 0;
}

上面这段程序发生错误在 str1=func(file)这行
也就是func()函数定义的问题,涉及到流类型 对象file
就这个地方总是编译不通过,错误如附件
有没有大侠指点一下啊?
谢谢

我这样编译就没错了,你试试

#include <fstream>
#include <iostream>
#include <cstring>
using namespace std;
char *func(ifstream file,char *str)
{
char line[256];
while(!file.eof())
{
file.getline(line,256);
strcpy(str,line);
}
return str;
}

int main()
{
ifstream file("example.txt");
if(!file.is_open())
{
cout<<"error\n";
}
else
{
char *str1=new char[20];
char *str=new char[20];
str1=func(file,str);
cout<<str1;
delete str;
delete str1;
}
getchar();
return 0;
}