2011-09-25 79 views
0

这是打开文件输入的正确方法吗?这是打开输入文件的正确方法吗?

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构造函数。

我的编译器出错了,或者我只是在我的参数中使用了错误的类型?

回答

2

不使用指针。这里不需要。

试试这个:

ifstream file(path); 

,然后用它作为:

//... 
file.getline(buffer, line_number);//file->getline(buffer, line_number); 
//... 
+0

我想已经:\。错误仍然是一样的,告诉我我不能将路径转换为ifstream对象或其他东西。 – zeboidlund

+2

@荷兰,你有没有'#include ',并且在你尝试时使用'std :: ifstream'? –

+0

是的,出于某种原因现在它可以工作,但无论什么原因,当我测试它。我得到一个std :: bad_alloc运行时错误。这里是我的主代码: std :: string bs [] = {“test”,“test2”,“test3”}; BinaryTree tree(bs,1); tree.read(“questions.txt”,1); – zeboidlund

0

没有指针需要,为@Nawaz说:

ifstream *file(path); 

潜在的内存泄漏:

char *buffer = new char[length]; 

你应该事后delete[]它:

delete[] buffer; 

... ,它是一个更容易使用std::string

std::string buffer; 

最终代码:

std::string BinaryTree::read(std::string path, int line_number) 
{ 
    std::string buf; 
    ifstream file(path.c_str()); 

    if(file.is_open()) 
    { 
     // file.seekg(0, std::ios::beg); // @Tux-D says it's unnecessary. 
     file.getline(buf, line_number); 
     file.close(); 
    } 

    return buf; 
} 
+0

除非他使用C++ 11,否则ifstream不接受std :: string,但只能使用char * –

+0

我正在使用QT创建器 - 我不确定是否使用C++ 11。 – zeboidlund

+0

@Holland我修改了代码以使用良好的'C++ 03'。你真的应该修复内存泄漏。 –

1
ifstream * file(path); //error: cannot convert ‘char*’ to ‘std::ifstream*’ in initialization 

问题是该对象的构造不合适。你可能会尝试做以下(或类似的东西),实际上传递一个字符数组的ifstream的对象的构造函数:

ifstream file(path); 

然而,引入一个星号的位置改变了全部意义。您正在创建一个指向对象ifstream的指针,但不是对象ifstream本身。而为了构建一个指针,你需要另一个指向ifstream对象的指针(即一个相同类型的指针)。

ifstream file(path); 
ifstream * ptr(&path); 

这是不是你打算做,反正,你可能想创建一个指针引用一个ifstream对象:

ifstream * file = new ifstream(path); 
//... more things... 
file->close(); 

但请记住,当它是对象必须free'd不再需要了。由指针引用的对象不会像普通(堆栈中的对象)对象那样自动释放。

ifstream * file = new ifstream(path); 
//... more things... 
file->close(); 
delete file; 

希望这会有所帮助。

0

的东西夫妇我会改变:

void BinaryTree::read(char * path, int line_number) 
{ 
    // Use an object not a pointer. 
    ifstream*  file(path); 

    // When you open it by default it is at the beginning. 
    // So we can remove it. 
    file->seekg(0, std::ios::beg); 

    // Doing manually memory line management. 
    // Is going to make things harder. Use the std::string 
    int length = file.tellg(); 
    char * buffer = new char[length]; 
    file.getline(buffer, line_number); 

    // Printing the line use the C++ streams. 
    printf("%d", length); 

    // DO NOT manually close() the file. 
    // When the object goes out of scope it will be closed automatically 
    // http://codereview.stackexchange.com/q/540/507 
    file->close(); 

} // file closed here automatically by the iostream::close() 

这里简化为:

void BinaryTree::read(char * path, int line_number) 
{ 
    ifstream  file(path); 

    std::string line; 
    std::getline(file, line); 

    std::cout << line.size() << "\n"; 
} 
0

你有比上面虽然提到的那些问题比较多:

  • 你使用文件作为指针和对象(file-> seekg(...)和file.tellg())
  • 在使用seekg移动到文件开头并使用该位置为您提供缓冲区大小后,您正在使用tellg。这会给你一个空的缓冲区(最好是指向零字节的指针,我认为)。
  • 然后,您使用作为参数传递的line_number参数调用getline。这会导致getline读取最多的line_number字节,但是您只分配了长度字节(如上所示,它为零)。我想你想读第line_number行,但需要更多的工作 - 你必须计算line_number换行符,直到你到达正确的位置。
  • 更为普遍的是,您希望在每次获得下一行时打开和关闭文件 - 您可能希望重新考虑整个界面。

道歉,如果我错过了这里的点

相关问题