这是打开文件输入的正确方法吗?这是打开输入文件的正确方法吗?
void BinaryTree::read(char * path, int line_number)
{
ifstream * file(path); //error: cannot convert ‘char*’ to ‘std::ifstream*’ in initialization
file->seekg(0, std::ios::beg);
int length = file.tellg();
char * buffer = new char[length];
file->getline(buffer, line_number);
printf("%d", length);
file->close();
}
我猜没有,因为编译器不会接受char
阵列,或为ifstream
构造一个std::string
,但是当我读到documentation,我看到string
S和/或char
阵列传递给ifstream
构造函数。
我的编译器出错了,或者我只是在我的参数中使用了错误的类型?
我想已经:\。错误仍然是一样的,告诉我我不能将路径转换为ifstream对象或其他东西。 – zeboidlund
@荷兰,你有没有'#include',并且在你尝试时使用'std :: ifstream'? –
是的,出于某种原因现在它可以工作,但无论什么原因,当我测试它。我得到一个std :: bad_alloc运行时错误。这里是我的主代码: std :: string bs [] = {“test”,“test2”,“test3”}; BinaryTree tree(bs,1); tree.read(“questions.txt”,1); – zeboidlund