fstream.read() 根本不读取任何内容
我正在尝试读取 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您从未为
somebuf
分配任何内容:因此,它不指向任何地方。
或者:
You never allocated anything for
somebuf
:therefore, it doesn't point anywhere.
Alternatively:
初始化缓冲区:
Initialize the buffer: