简体   繁体   中英

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?

It depend on what you mean by "work"

You can think of those files as a book written in Greek .

If you want to just mess with binary representation ( display text in Greek on screen ) then yes, you can do that.

If you want to actually extract some info: edit video stream, remove voice from audio ( actually understand what is written ), then you would need to either parse file format yourself ( learn Greek ) or use some specialized library ( hire a translator ).

Either way, filestreams are suited to actually access those files data (and many libraries do use them under the hood)

You can open these files using fstream, but the important thing to note is you must be intricately aware of what is contained within the file in order to process it.

If you just want to open it and spit out junk, then you can definitely just start at the first line of the file and exhaustively push all data into your console.

If you know what the file looks like on the inside, then you can process it just as you would any other file.

There may be specific libraries for processing specific files, but the fstream library will allow you to access any file you'd like.

All files are just bytes. There's nothing stopping you from reading/writing those bytes however you see fit.

The trick is doing something useful with those bytes. You could read the bytes from a .jpg file, for example, but you have to know what those bytes mean, and that's complicated . Usually it's best to use libraries written by people who know about the format in question, and let them deal with that complexity.

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

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