简体   繁体   English

用C ++读写二进制文件

[英]Reading and writing binary files in C++

I am entirely new to C++ with just a couple of hours self-teaching, that started yesterday. 我是一个全新的C ++,只用了几个小时的自学,从昨天开始。 SO: 所以:

I have a uncompressed simple beep.wav file, just about 3 seconds long with a single beeping sound in it. 我有一个未压缩的简单beep.wav文件,只有大约3秒长,只有一声哔哔声。

What I am ultimately trying to achieve is, just to read the file, and at the same time write the binary data, all including: the header , ftm and data or all that is hex readable data and dump it into a simple beep.txt file. 我最终想要实现的只是读取文件,同时写入二进制数据,包括: 头文件ftm数据或所有十六进制可读数据,并将其转储到一个简单的beep.txt文件。

is this possible, in C++? 这是可能的,在C ++中? If so, how should I begin reading and dumping the binary file? 如果是这样,我应该如何开始阅读和转储二进制文件?

I can read the file ~more or less, using the <fstream> class 我可以使用<fstream>类或多或少地读取文件

#include <iostream>
#include <fstream>

   int main()
   {
       ifstream myfile ("beep.wav", ios::in|ios::binary|ios::ate);
       if (myfile.is_open())
       {
        //reading goes here
        }
       return 0;
    }

As mentioned by yzt , you need to know by advance what's in your .wav and in which order. 正如yzt所提到的,你需要提前知道.wav中的内容以及顺序。 As far as I know, you will have tags, headers and values (samples) as well as compression informations. 据我所知,您将拥有标签,标题和值(样本)以及压缩信息。 So, to give a start : 所以,开始:

If you know, by example, that the very first thing to do is to read the compression rate, you'll start your reading process by extracting may be a double : 如果您知道,例如,首先要做的是读取压缩率,您将通过提取开始阅读过程可能是一个double

ifstream myfile("beep.wav", ios::in|ios::binary);

double compression_rate;
myfile.read((char*)&compression_rate, sizeof(compression_rate));

// As well as probably the sample rate...
double sample_rate;
myfile.read((char*)&sample_rate, sizeof(sample_rate));

Then, may be, the number of samples : 然后,可能是,样本数量:

int nb_samples;
myfile.read((char*)&nb_samples, sizeof(nb_samples));

Then, the values for those samples... (here stored as a vector of double ) 然后,那些样本的值...(这里存储为doublevector

vector<double> vect;
vect.resize(nb_samples);
myfile.read((char*)&vect[0], nb_samples * sizeof(double));

Etc... 等等...

But again, what the .wav you opened is made of ? 但是,你打开的.wav又是什么?

Once you completely master the content, you can go the other way and start writing your own .wav from scratch... 一旦你完全掌握了内容,你可以走另一条路,开始自己编写自己的.wav ...


Getting started - and here . 入门 - 在这里

Bytes - "Autopsy" of a PCM Wav file . 字节 - PCM Wav文件的“Autopsy”

This example will read the data from the input file and write it as 2-character hex values (text). 此示例将从输入文件中读取数据并将其写为2个字符的十六进制值(文本)。

ofstream outfile("beep.txt");
outfile << hex << setfill('0');

char c;
while( outfile.good() && myfile.get(c) )
{
    outfile << setw(2) << +(unsigned char)c;
}

The WAV file format specification tells you the binary layout of WAV files - you need this because this tells you what bytes to read in what order. WAV文件格式规范告诉您WAV文件的二进制布局 - 您需要这个,因为它告诉您按什么顺序读取哪些字节。 (You'll really need to read int -s and various other fields in addition to bytes.) Once you read this information, you can translate it for display and write it out to a text file. (除了字节之外,你真的需要读取int -s和各种其他字段。)一旦你阅读了这些信息,你就可以将其翻译出来进行显示并将其写入文本文件。 For example, you can write out the length (duration) of the sound in the WAV file by reading the header (I don't know the exact layout) and then calculate how many seconds that translates to. 例如,您可以通过读取标题(我不知道确切的布局)来写出WAV文件中声音的长度(持续时间),然后计算转换为的秒数。

In technical terms all other answers to your question are good - they give you pointers on how to read / write binary & text data in C++. 在技​​术方面,您问题的所有其他答案都很好 - 它们为您提供了如何在C ++中读/写二进制和文本数据的指针。

You can read the entire file into a buffer: 您可以将整个文件读入缓冲区:

  1. Seek to the end of the file 寻找文件的末尾
  2. Make a buffer big enough to fit the file 制作足够大的缓冲区以适合文件
  3. Seek back to the start of the file 回到文件的开头
  4. Read the entire file into the buffer 将整个文件读入缓冲区

:

myfile.seekg(0, std::ios::end);
std::string buffer(myfile.tellg(), '\0');
myfile.seekg(0);

myfile.read(&buffer.front(), buffer.size());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM