Search code examples
c++iofile-type

How do I input and output various file types in c++


I've seen a lot of examples of i/o with text files I'm just wondering if you can do the same with other file types like mp3's, jpg's, zip files, etc..?

Will iostream and fstream work for all of these or do I need another library? Do I need a new sdk?

It's all binary data so I'd think it would be that simple. But I've been unpleasently surprised before.

Could I convert all files to text or binary?


Solution

  • You can work on binary streams by opening them with openmode binary :

    ifstream ifs("mydata.mp3", ios_base::binary); 
    

    Then you read and write any binary content. However, if you need to generate or modify such content, play a video or display a piture, the you you need to know the inner details of the format you are using. This can be exremely complex, so a library would be recomended. And even with a library, advanced programming skills are required.

    Examples of open source libraries: ffmpeg for usual audio/video format, portaudio for audio, CImg for image processing (in C++), libpng for png graphic format, lipjpeg for jpeg. Note that most libraries offer a C api.

    Some OS also supports some native file types (example, windows bitmaps).