Using Binary and Text Modes

Asked 2 years ago, Updated 2 years ago, 69 views

How do I use binary and text modes differently?
What are the differences?

http://www7b.biglobe.ne.jp/~robe/cpphtml/ I was not satisfied with this explanation

c

2022-09-30 16:26

2 Answers

How you define text mode is OS dependent.For example, UNIX does not specifically require switching modes because text mode is considered the same as binary mode.
For Windows text mode, the newline code is represented using two bytes: \r\n (CRLF) when saving to a file.However, the C program requires conversion when reading and writing to use one byte of \n (NL).This translation is

FILE*fp;
Converted to fprintf(fp, "\n"; // "\r\n"

In addition to being line-oriented and straightforward as shown in , it is also mechanically translated into binary functions such as fread and fwrite.


2022-09-30 16:26

Binary mode is used when you want to handle the contents of the file you want to read and write without changing a single byte. Text mode is used when you want to share source code on different platforms.

The appropriate "line feed code in text file" for the platform (execution system) is different.
What is the difference between \n and \r\nin a new line?
Text mode automatically selects this "appropriate line feed code."
In text mode, the \n description and "appropriate line feed code" in the source are always inter-translated.

In binary mode, \n in the source file is defined as `\x0A' in the data file.
That is, it does not convert the value (at will.As a result, you can read and write data files as they are.


2022-09-30 16:26

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.