Binary vs. Text Mode

What is the difference between binary mode ("wb" or "rb") and text mode ("w" or "r") when opening a file with SDL_RWFromFile()? Why is it important to use binary mode?

When you open a file using SDL_RWFromFile(), you can specify whether to open it in text mode or binary mode. The difference between these modes lies in how they handle special characters, particularly newline characters.

Text Mode ("w" or "r"):

Newline Translation: In text mode, the operating system may translate newline characters to ensure that text files are stored in a format compatible with the system's conventions.

For example, on Windows, newline characters are typically represented by a carriage return followed by a line feed (\r\n), while on Linux and macOS, they are represented by a single line feed (\n).

When you write a \n character in text mode, the operating system might automatically convert it to \r\n on Windows. Conversely, when you read a \r\n sequence, it might be translated back to a single \n.

Character Encoding: Text mode also assumes that the file contains text encoded using a specific character encoding (e.g., UTF-8, ASCII). The operating system might perform some character encoding conversions when reading or writing data.

Binary Mode ("wb" or "rb"):

No Translation: In binary mode, no translation or conversion is performed. Data is read and written exactly as it is, byte for byte. Newline characters and other special characters are treated like any other byte of data.

Raw Data: Binary mode is suitable for handling any type of data, not just text. You can use it to read and write images, audio files, executable files, or any other type of file where you need to preserve the exact byte sequence.

Importance of Binary Mode

In most cases, including the high score example in this lesson, we're storing an integer value (or an array of integers) in the file. Integers are not text; they are binary data. If we were to open the file in text mode, the following problems could occur:

Corruption of Data: When we write the integer to the file, the operating system might try to interpret it as text and perform newline translations or character encoding conversions.

This would corrupt the binary representation of the integer, making it unreadable when we try to load it back into the program.

Incorrect Interpretation: When we read the integer back from the file, the operating system might again try to interpret it as text, potentially translating newline sequences or performing character encoding conversions.

This would result in an incorrect integer value being read.

Example

Consider the following code:

#include <SDL.h>
#include <iostream>

int main() {
  int32_t Score{12345};

  // Write in text mode
  SDL_RWops* TextFile{SDL_RWFromFile(
    "text_mode.dat", "w")};
  SDL_RWwrite(
    TextFile, &Score, sizeof(int32_t), 1);
  SDL_RWclose(TextFile);

  // Write in binary mode
  SDL_RWops* BinaryFile{SDL_RWFromFile(
    "binary_mode.dat", "wb")};
  SDL_RWwrite(
    BinaryFile, &Score, sizeof(int32_t), 1);
  SDL_RWclose(BinaryFile);

  return 0;
}

If you were to open text_mode.dat in a hex editor, you might see unexpected bytes due to newline translations or character encoding issues.

On the other hand, binary_mode.dat would contain the exact binary representation of the integer 12345.

Therefore, it's crucial to use binary mode in the high score example (and in most cases where you're not specifically dealing with text files) to ensure that the data is written and read correctly without any unintended modifications.

Read/Write Offsets and Seeking

Learn how to manipulate the read/write offset of an SDL_RWops object to control stream interactions.

Questions & Answers

Answers are generated by AI models and may not have been reviewed. Be mindful when running any code on your device.

Seeking Past EOF
If we seek past the end of a file using SDL_RWseek(), what happens when we try to read or write data? Will it extend the file, or will it result in an error? How can we use this behaviour to append to a file?
Multiple High Scores
How can we modify the high score example to store multiple high scores (e.g., the top 10) instead of just one?
Closing SDL_RWops
Why is it important to close the SDL_RWops using SDL_RWclose() after we're finished with it? What are the potential consequences of not closing it?
Storing Player Name
How could we modify the high score example to also store the player's name along with the score?
Searching Large Files
If we have a very large file, how can we efficiently search for a specific piece of data without reading the entire file into memory?
Or Ask your Own Question
Purchase the course to ask your own questions