Reading and writing binary files in C++

感情迁移 提交于 2019-12-07 20:40:41

问题


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;
    }

回答1:


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.




回答2:


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;
}



回答3:


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




回答4:


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


来源:https://stackoverflow.com/questions/17139149/reading-and-writing-binary-files-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!