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

发布于 2024-12-06 16:36:45 字数 688 浏览 0 评论 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当我阅读 文档 时,我看到 string 和/或 char 数组被传递给ifstream 构造函数。

我的编译器有问题还是我只是在参数中使用了错误的类型?

Is this the proper way to open a file for input?

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();

}

I'm guessing not, because the compiler won't accept a char array, or a std::string for the ifstream constructor, yet when I read documentation, I see strings and/or char arrays being passed to ifstream constructors.

Is something wrong with my compiler or am I just using the wrong type in my parameter?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

恬淡成诗 2024-12-13 16:36:45

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

试试这个:

ifstream file(path);

然后将其用作:

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

Dont use pointer. It is not needed here.

Try this:

ifstream file(path);

and then use it as:

//...
file.getline(buffer, line_number);//file->getline(buffer, line_number);
//...
孤芳又自赏 2024-12-13 16:36:45
ifstream * file(path); //error: cannot convert ‘char*’ to ‘std::ifstream*’ in initialization

问题在于对象的构造不合适。您可能正在尝试执行以下操作(或类似的操作),实际上将 char 数组传递给 ifstream 对象的构造函数:

ifstream file(path);

但是,此处星号的引入改变了整个含义。您正在创建一个指向对象 ifstream 的指针,而不是对象 ifstream 本身。为了构造一个指针,您需要另一个指向 ifstream 对象的指针(即相同类型的指针)。

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

这不是您想要做的,无论如何,您可能想创建一个由指针引用的 ifstream 对象:

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

但请记住,当不再需要该对象时,必须释放该对象。指针引用的对象不会像普通(堆栈中的对象)对象那样自动释放。

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

希望这有帮助。

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

The problem is that the construction of the object is not appropriate. You're probably trying to do the following (or something similar), indeed passing a char array to the constructor of the ifstream object:

ifstream file(path);

However, the introduction of an asterisk here changes the whole meaning. You're creating a pointer to an object ifstream, but not the object ifstream itself. And in order to construct a pointer, you would need another pointer to an ifstream object (i.e. a pointer of the same type).

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

This is not what you intended to do, anyway, you probably wanted to create an ifstream object referenced by a pointer:

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

But remember that the object must free'd when it is not needed anymore. Objects referenced by pointers are not automatically free'd as it happens with normal (objects in the stack) objects.

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

Hope this helps.

两人的回忆 2024-12-13 16:36:45

不需要指针,正如 @Nawaz 所说:

ifstream *file(path);

潜在的内存泄漏:

char *buffer = new char[length];

您应该随后删除它:

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;
}

No pointer needed, as @Nawaz said:

ifstream *file(path);

Potential memory leak:

char *buffer = new char[length];

You should delete[] it afterwards:

delete[] buffer;

...But, it's a lot easier to use std::string:

std::string buffer;

Final code:

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;
}
眼泪都笑了 2024-12-13 16:36:45

不过,您遇到的问题比上面提到的问题更多:

  • 您以不同方式使用 file 作为指针和对象(file-> seekg(...) 和 file.tellg()),
  • 您正在使用该位置使用eekg移动到文件开头后使用tellg,然后使用该位置给出缓冲区的大小。这将为您提供一个空缓冲区(我认为最多是一个指向零字节的指针)。
  • 然后,您可以使用作为参数传递的 line_number 参数来调用 getline 。这将导致 getline 最多读取 line_number 个字节,但您只分配了 length 个字节(如我们上面所见,该字节为零)。我猜你想读取第 line_number 行,但这需要更多的工作 - 你必须计算 line_number 换行符,直到到达正确的行。
  • 更一般地说,您是否希望在每次收到下一行时打开和关闭文件 - 您可能需要重新考虑整个界面。

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

You've got more problems than the ones mentioned above though:

  • you're using file variously as a pointer and an object (file->seekg(...) and file.tellg())
  • you're taking the position with the tellg after having moved to the start of the file with the seekg and then using that position to give you the size of the buffer. This will give you an empty buffer (at best a pointer to zero bytes, I think).
  • you're then calling getline with the line_number parameter passed as an argument. This will cause getline to read at most line_number bytes but you've only allocated length bytes (which is zero as we've seen above). I guess that you want to read the line_number'th line but that takes a bit more work - you've gotta count up line_number newlines until you reach the right line.
  • more generally do you want to open and close the file each time you get the next line - you might want to rethink your interface as a whole.

apologies if I've missed the point here

恍梦境° 2024-12-13 16:36:45

我要改变的几件事:

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";
} 

Couple of things I would change:

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()

Simplified here:

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

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

    std::cout << line.size() << "\n";
} 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文