简体   繁体   中英

Reading and writing binary files in C++

I am entirely new to C++ with just a couple of hours self-teaching, that started yesterday. SO:

I have a uncompressed simple beep.wav file, just about 3 seconds long with a single beeping sound in it.

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.

is this possible, in 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

#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. 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 :

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 )

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 ?

Once you completely master the content, you can go the other way and start writing your own .wav from scratch...


Getting started - and here .

Bytes - "Autopsy" of a PCM Wav file .

This example will read the data from the input file and write it as 2-character hex values (text).

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

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++.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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