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

C++ fstream.read()根本不读取任何内容,c++,binaryfiles,fstream,C++,Binaryfiles,Fstream,我正在尝试读取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文件的第一行(我编辑了这个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;
}
#包括
#包括
#包括
使用名称空间std;
int main()
{
流媒体mp3;
打开(“05 Imagine.mp3”,ios::binary | ios::in | ios::out);
/*mp3.seekg(0,ios::end);
int lof=mp3.tellg();

难道你从来没有给某个人分配过任何东西吗

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';//不确定是否需要null终止。。。
while(mp3.read(somebuf,10))//读取前10个字符,即“我是一个mp3文件”。
{

//难道你从来没有给某个人分配过任何东西吗

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';//不确定是否需要null终止。。。
while(mp3.read(somebuf,10))//读取前10个字符,即“我是一个mp3文件”。
{
//cout初始化缓冲区:

char somebuf[10];
    while(mp3.read(somebuf, 10)) //Read the first 10 chars which are "I'm an MP3 file".
    {
        //cout << somebuf;
    }
charsomebuf[10];
while(mp3.read(somebuf,10))//读取前10个字符,即“我是一个mp3文件”。
{
//cout初始化缓冲区:

char somebuf[10];
    while(mp3.read(somebuf, 10)) //Read the first 10 chars which are "I'm an MP3 file".
    {
        //cout << somebuf;
    }
charsomebuf[10];
while(mp3.read(somebuf,10))//读取前10个字符,即“我是一个mp3文件”。
{

//前十个字符可能是“我是一个MP3”,前十个字符可能是“我是一个MP3”,不要忘记
delete[]
it after:)或者,你可以使用cstring:
char somebuf[10]={};
。我添加了
={}
将字符串元素初始化为空字符,这在这里不是必需的,但通常是很好的做法。@thekashyap他想打印出来。我不确定是否需要以空结尾。啊,我看到你比我强:P@thekashyap:你需要11个字符才能插入“空字符”在字符串末尾。如果字符串没有空字符,您将无法输出它或使用函数,如
strcpy
strlen
等,因为如果字符串没有空字符终止它,它们无法确定字符串的结尾。在添加
somebuf[10]后='\0';
这很有意义:)@ChrisParton虽然你大体上是对的,但我通常希望在二进制模式下打开的文件的所有内容(读/写/缓冲/复制/contcat/…)都由程序员自己严格控制。就像使用
strncpy
一样,不要忘记
删除[]
it after:)或者,您可以使用cstring:
charsomebuf[10]={};
。我添加了
={}
将字符串元素初始化为空字符,这在这里不是必需的,但通常是很好的做法。@thekashyap他想打印出来。我不确定是否需要以空结尾。啊,我看到你比我强:P@thekashyap:你需要11个字符才能插入“空字符”在字符串末尾。如果字符串没有空字符,您将无法输出它或使用函数,如
strcpy
strlen
等,因为如果字符串没有空字符终止它,它们无法确定字符串的结尾。在添加
somebuf[10]后='\0';
这很有意义:)@ChrisParton虽然你大体上是对的,但我通常希望在二进制模式下打开的文件的所有内容(读/写/缓冲区/复制/contcat/…)都由程序员自己严格控制..就像使用
strncpy