简体   繁体   English

如何在C ++中输入和输出各种文件类型

[英]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..? 我已经看到了很多文本文件输入/输出的示例,我只是想知道您是否可以对其他文件类型(例如mp3,jpg,zip文件等)执行相同的操作?

Will iostream and fstream work for all of these or do I need another library? iostream和fstream会为所有这些工作吗,还是我需要另一个库? Do I need a new sdk? 我需要新的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. 您可以使用fstream打开这些文件,但是要注意的重要一点是,您必须完全了解文件中包含的内容才能进行处理。

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. 可能有用于处理特定文件的特定库,但是fstream库将允许您访问所需的任何文件。

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 . 例如,您可以从.jpg文件中读取字节,但是您必须知道这些字节的含义,这很复杂 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 : 您可以通过使用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. 开源库的示例: ffmpeg用于常规音频/视频格式, portaudio用于音频, CImg用于图像处理(在C ++中), libpng用于png图形格式, lipjpeg用于jpeg。 Note that most libraries offer a C api. 请注意,大多数库都提供C API。

Some OS also supports some native file types (example, windows bitmaps ). 某些操作系统还支持某些本机文件类型(例如Windows位图 )。

暂无
暂无

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

相关问题 基于输入的各种类型的 C++ 模板 - C++ templates with various types based on input 我应该如何读入一个文本文件,其中每一行都包含C ++中的各种数据类型? - How should I go about reading in a text file with each line containing various data types in C++? 具有各种变量的C ++文件输入 - C++ File input with various variables 我有2个文本文件,output文本文件和输入文本文件,我怎么append输入文本文件在output文本文件开头的行(c++)? - I have 2 text files , output text file and input text file , how do I append the lines of input text file at the beginning of output text file(c++)? 在C ++中允许多种类型的输入用于各种形式的“是”和“否” - Allowing multiple types of input for various forms of “yes” and “no” in C++ 如何在C ++中将批处理文件的输出读取为字符串 - How do I read the output of a batch file into a string in C++ 我如何从文件C ++读取并输出它 - How do i read from a file C++ and output it 如何将文本文件输入转换为 c++ 中的数组 - How do i convert a text file input to an array in c++ 如何从输入读取 CSV 行并将每个值初始化为不同类型的变量? (C++) - How do I read a CSV line from input and initialize each value to variables of different types? (C++) 各种数据类型的变量如何以C ++(本地)二进制形式存储? - How are variables of various data types stored in C++(native) binary?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM