fstream.read() 根本不读取任何内容

发布于 2024-12-21 18:57:59 字数 763 浏览 2 评论 0原文

我正在尝试读取 MP3 文件的第一行(我编辑了此 mp3 文件以在文件开头包含文本“我是 MP3”)。

这就是我想做的:

#include <iostream>
#include <string>
#include <fstream>
using namespace std;

int main()
{
    fstream mp3;
    mp3.open("05 Imagine.mp3", ios::binary | ios::in | ios::out);
    /*mp3.seekg(0, ios::end);
    int lof = mp3.tellg();
    cout << "Length of file: " << lof << endl;
    mp3.seekg(0, ios::beg);*/

    //char ch;
    //cout << mp3.get(ch) << endl;

    char* somebuf;
    while(mp3.read(somebuf, 10)) //Read the first 10 chars which are "I'm an MP3 file".
    {
        //cout << somebuf;
    }
    return 0;
}

出于某种原因,这正在崩溃。在某些时候它没有崩溃,但是当我执行 cout << 时它没有打印任何内容。一些缓冲区。有人可以帮我解决这个问题吗?

I'm trying to read the first line of an MP3 file (I edited this mp3 file to contain the text "I'm an MP3" right at the beginning of the file).

This is what I'm trying to do:

#include <iostream>
#include <string>
#include <fstream>
using namespace std;

int main()
{
    fstream mp3;
    mp3.open("05 Imagine.mp3", ios::binary | ios::in | ios::out);
    /*mp3.seekg(0, ios::end);
    int lof = mp3.tellg();
    cout << "Length of file: " << lof << endl;
    mp3.seekg(0, ios::beg);*/

    //char ch;
    //cout << mp3.get(ch) << endl;

    char* somebuf;
    while(mp3.read(somebuf, 10)) //Read the first 10 chars which are "I'm an MP3 file".
    {
        //cout << somebuf;
    }
    return 0;
}

For some reason, that is crashing. At some point it didn't crash, but it didn't print anything when I did cout << somebuf. Can someone help me with this?

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

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

发布评论

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

评论(2

新雨望断虹 2024-12-28 18:57:59

您从未为 somebuf 分配任何内容:

char* somebuf;

因此,它不指向任何地方。

char* somebuf = new char[11];
somebuf[10] = '\0';          //  Not sure if it is necessary to null-terminate...
while(mp3.read(somebuf, 10)) //  Read the first 10 chars which are "I'm an MP3 file".
{
    //cout << somebuf;
}


//  and free it later
delete [] somebuf;

或者:

char somebuf[11];
somebuf[10] = '\0';          //  Not sure if it is necessary to null-terminate...
while(mp3.read(somebuf, 10)) //  Read the first 10 chars which are "I'm an MP3 file".
{
    //cout << somebuf;
}

You never allocated anything for somebuf:

char* somebuf;

therefore, it doesn't point anywhere.

char* somebuf = new char[11];
somebuf[10] = '\0';          //  Not sure if it is necessary to null-terminate...
while(mp3.read(somebuf, 10)) //  Read the first 10 chars which are "I'm an MP3 file".
{
    //cout << somebuf;
}


//  and free it later
delete [] somebuf;

Alternatively:

char somebuf[11];
somebuf[10] = '\0';          //  Not sure if it is necessary to null-terminate...
while(mp3.read(somebuf, 10)) //  Read the first 10 chars which are "I'm an MP3 file".
{
    //cout << somebuf;
}
橙味迷妹 2024-12-28 18:57:59

初始化缓冲区:

char somebuf[10];
    while(mp3.read(somebuf, 10)) //Read the first 10 chars which are "I'm an MP3 file".
    {
        //cout << somebuf;
    }

Initialize the buffer:

char somebuf[10];
    while(mp3.read(somebuf, 10)) //Read the first 10 chars which are "I'm an MP3 file".
    {
        //cout << somebuf;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文