C ++中的二进制文件和文本文件有什么区别?两种程序在这两种模式之间没有区别.为什么? [英] What is the difference between binary and text files in c++? The two programs do not differ between these two modes. why?

查看:83
本文介绍了C ++中的二进制文件和文本文件有什么区别?两种程序在这两种模式之间没有区别.为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C ++中的二进制文件和文本文件有什么区别?两种程序在这两种模式之间没有区别.为什么?
二进制模式:---

 #include   <   iostream.h  > 
 #include   <   fstream.h  > 
 #include   <   conio.h  > 

 int  main()
{
    fstream f;
    f.open(" ,ios :: out | ios :: binary) ;
    f<< " ;
    f.close();
    
    getch();
    返回  0 ;
} 


文字模式:----

 #include   <   iostream.h  > 
 #include   <   fstream.h  > 
 #include   <   conio.h  > 

 int  main()
{
    fstream f;
    f.open(" ,ios :: out);
    f<< " ;
    f.close();
    
    getch();
    返回  0 ;
}</conio.h>/fstream.h>/iostream.h</conio.h>/fstream.h>/iostream.h< div类="h2_lin">解决方案

这是新手之间最令人困惑的争论之一,因为二进制数据"的概念通常由不同的出版物以不同的含义来引用.

特别地,< iostream>是什么.库指的是文本",二进制"模式处理字符翻译".

C ++流被定义为几乎无限的符号序列,称为字符".

在文本模式"下,某些控制字符将根据底层操作系统的需求进行解释.例如,在 Windows 中,CR 字符被替换为CR/LF 序列.
在二进制模式"下,不进行任何转换,并且在将字符提供给流本身时将其写入.

在您的情况下,可以通过在字符串中写入一些 "\r" "\n"并使用十六进制编辑器查看结果文件的外观来证明这一点.


这个概念与另一个概念完全无关,后者是数据的表示形式(尤其是数字):它可以以文本形式出现(数字123被视为字形"1""2"的序列)和"3"一个接一个,每个都由其自己的代码点表示)或采用其他某种编码方式(例如二进制形式),可以与数据本身在计算机内存中的表示方式相同(123 = 0111_1011 = 0x7B).

但这不是通过std::ostream<<运算符写数字所得到的:您必须在binary_mode流上使用ostream未格式化的方法(put write)才能进行二进制编码的转储将一块内存存储到一个文件中.
请注意,如果流是在文本模式下,写操作是相同的,但是转储的每个字节都将根据基础操作系统的文本表示规则进行相应的解释和转换(因此,将表示弄乱了).

您只输出字符串,怎么能是二进制或非二进制?我认为这应该可以解决您对此代码的困惑.标志ios:binary不能影响它.

基本上,数据不是分类为二进制的,也不是分类为二进制的,因为所有内容都是二进制的.基本上,文本是二进制数据的一种特殊形式.当您使用十进制或十六进制数字以及其他字符"0-9","A"-"F","0x"以文本形式显示数字数据时,真正的区别就发挥了作用. ,"E",-","+".使用这种表示形式的数据格式通常称为文本"格式,而不是二进制"格式,但是没有基本分类.同样,所有数据都是二进制.

—SA


ascii模式会将\ r \ n转换为\ r,而二进制文件则不会


What is the difference between binary and text files in c++? The two programs do not differ between these two modes. why?
Binary Mode:---

#include<iostream.h>
#include<fstream.h>
#include<conio.h>

int main()
{
    fstream f;
    f.open("E:\\Text1",ios::out|ios::binary);
    f<<"Hello how are you?";
    f.close();
    
    getch();
    return 0;
}


Text Mode:----

#include<iostream.h>
#include<fstream.h>
#include<conio.h>

int main()
{
    fstream f;
    f.open("E:\\Text1",ios::out);
    f<<"Hello how are you?";
    f.close();
    
    getch();
    return 0;
}</conio.h></fstream.h></iostream.h></conio.h></fstream.h></iostream.h>

解决方案

This is one of the most confusing arguments between newbies, since the concept of "binary data" is often referred by different publications, with different meanings.

In particular, what the <iostream> library refer to "text" and "binary" mode deals with the "character translation".

A C++ stream is defines as a virtually infinite sequence of symbols named "characters".

In "text mode" certain control characters are interpreted according to the underlying operating system needs. For example, in Windows, the CR character is replaced by a CR/LF sequence.
In "binary mode" no translation is done, and characters are written as they are supplied to the stream itself.

In your case this can be made evident by writing some "\r" and "\n" into your strings, and looking at (with an hex editor) how the resulting files look.


This concept is completely unrelated from another concept, that is the representation of data (numbers, in particular): it may happen either in textual form (the number 123 viewed as the sequence of the glyph ''1'' ''2'' and ''3'' one after the other, each represented by its own code-point) or in some other coding like the binary form, that can be the same as the way the data themselves are represented inside the computer memory (123 = 0111_1011 = 0x7B).

But this is not what you will get by writing numbers with the << operator of an std::ostream: you must use the ostream unformatted methods(put and write) on a binary_mode stream to have a binary-encoded dump of a piece of memory into a file.
Note that, if the stream is in text mode, write works the same, but every byte you dump, will be interpreted and translated accordingly to the text representation rules of the underlying operating system (hence, messing up the representation).


You output just string, how can it be binary or not binary? I think it should resolve your confusion about this very code. The flag ios:binary cannot affect it.

Basically, data is not classified into binary and not binary, because everything is binary. Basically, text is a special form of binary data. The real difference comes into play when you present numeric data in the form of text using decimal or hexadecimal digits and other characters, ''0-9'', ''A''-''F'', ''0x'', ''E'', ''-'', ''+''. The data format using such presentation is usually called "text" format as opposed to "binary", but there is no fundamental classification; again, all data is binary.

—SA


ascii mode will translate \r\n into \r, while binary won''t


这篇关于C ++中的二进制文件和文本文件有什么区别?两种程序在这两种模式之间没有区别.为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆