🚀 Learn BLE on Linux (BlueZ) – Programming Part 1
Hands-on Bluetooth Low Energy course. Special price: ₹399
Enroll Now on Udemy →Text vs Binary Files
See exactly which bytes land on disk when you save the number 1679 as text and as binary, how newlines and end-of-file differ across systems, and which format to pick for embedded C programming.
A text file vs binary file in C comes down to one question: do you store the characters that print a value, or the raw bytes the value occupies in memory? Both are valid, and both are used daily in firmware and Linux tools. In this lecture of our free C programming course you will write the same number both ways, inspect the bytes with a hex dump, and learn the trade-offs. If you want to master embedded C programming, understanding this layout is essential, because sensor logs, calibration tables and firmware images all depend on it.
What You Will Learn
- How a text file vs binary file in C stores the same data differently
- Why the number 1679 needs different amounts of space in each
- How to look at real bytes with
od - Newline translation and the old Ctrl+Z end-of-file marker
- Speed, size and portability trade-offs, including byte order
- Which format to choose for logs, configs and records
Prerequisites
Lecture 1 of this chapter (streams and FILE *), and basic knowledge of hexadecimal numbers and fixed-width types from stdint.h.
Text File vs Binary File in C: Two Ways to Store Data
In text format, data is written as readable characters. A number becomes the sequence of digit characters that you would see on screen, and lines end with a newline. In binary format, data is copied to the file exactly as it sits in memory, with no conversion. Text files open in any editor. Binary files look like garbage in an editor and only a program that knows the layout can interpret them.
| Property | Text file | Binary file |
|---|---|---|
| Content | Characters (digits, letters, newlines) | Raw memory bytes |
| Readable in an editor | Yes | No |
| Typical functions | fprintf, fscanf, fgets, fputs | fwrite, fread |
| Conversion work | Number to characters and back | None |
| Size for small numbers | One byte per digit plus separators | Fixed size of the type |
| Portable between machines | High | Low unless you control layout |
| Open mode letter | "w", "r" | "wb", "rb" |
The Number 1679 in a Text File vs Binary File in C
The decimal value 1679 equals 0x068F in hexadecimal. Stored as text, the file holds the four characters ‘1’, ‘6’, ‘7’ and ‘9’, which in ASCII are the bytes 0x31 0x36 0x37 0x39. Stored as a 16-bit integer, it needs just the two bytes that make up 0x068F.
Why 2 bytes in older books but 4 here?
Many older C textbooks assume int is 16 bits. On modern PCs and on 32-bit ARM Cortex-M parts, int is 32 bits. The size of int depends on the compiler and target. When file layout matters, use fixed-width types such as uint16_t and int32_t.
Try it: write 1679 three ways
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <sys/stat.h>
static long file_size(const char *path)
{
struct stat st;
return stat(path, &st) == 0 ? (long)st.st_size : -1L;
}
static void must(FILE *fp, const char *name)
{
if (fp == NULL) {
perror(name);
exit(EXIT_FAILURE);
}
}
int main(void)
{
uint16_t v16 = 1679;
int32_t v32 = 1679;
FILE *fp;
fp = fopen("num.txt", "w");
must(fp, "num.txt");
fprintf(fp, "%d", 1679);
fclose(fp);
fp = fopen("num16.bin", "wb");
must(fp, "num16.bin");
fwrite(&v16, sizeof v16, 1, fp);
fclose(fp);
fp = fopen("num32.bin", "wb");
must(fp, "num32.bin");
fwrite(&v32, sizeof v32, 1, fp);
fclose(fp);
printf("num.txt (text, \"1679\") : %ld bytes\n", file_size("num.txt"));
printf("num16.bin (binary, uint16_t) : %ld bytes\n", file_size("num16.bin"));
printf("num32.bin (binary, int32_t) : %ld bytes\n", file_size("num32.bin"));
printf("sizeof(int) on this machine : %zu\n", sizeof(int));
/* newline handling: same string, two modes */
fp = fopen("nl_text.dat", "w");
must(fp, "nl_text.dat");
fputs("A\nB\n", fp);
fclose(fp);
fp = fopen("nl_bin.dat", "wb");
must(fp, "nl_bin.dat");
fputs("A\nB\n", fp);
fclose(fp);
printf("\"A\\nB\\n\" written in \"w\" mode : %ld bytes\n", file_size("nl_text.dat"));
printf("\"A\\nB\\n\" written in \"wb\" mode : %ld bytes\n", file_size("nl_bin.dat"));
return EXIT_SUCCESS;
}
$ gcc -Wall -Wextra -std=c11 -o t_vs_b t_vs_b.c
$ ./t_vs_b
num.txt (text, "1679") : 4 bytes
num16.bin (binary, uint16_t) : 2 bytes
num32.bin (binary, int32_t) : 4 bytes
sizeof(int) on this machine : 4
"A\nB\n" written in "w" mode : 4 bytes
"A\nB\n" written in "wb" mode : 4 bytes
$ od -An -tx1 num.txt
31 36 37 39
$ od -An -tx1 num16.bin
8f 06
$ od -An -tx1 num32.bin
8f 06 00 00
The hex dump proves the theory. The text file holds the ASCII codes of the four digits. The binary files hold 8f 06, which is 0x068F with the low byte first.
Byte order matters for binary files
The two bytes appear as 8f 06 because x86 PCs and most ARM systems are little-endian: the least significant byte goes first. A big-endian machine would write 06 8f. A binary file written on one and read on the other gives wrong numbers unless you convert. Text format avoids the problem because the characters are the same everywhere.
Newlines and End of File
Newline translation
In memory, a line break is the single character '\n' (value 10). Some systems store it differently on disk. Classic Windows and DOS text files use the pair carriage return plus line feed ('\r' and '\n', values 13 and 10). The C runtime there translates when a file is opened in text mode: '\n' becomes two bytes on write and the pair collapses back to one on read. In binary mode no translation takes place.
On Linux and other POSIX systems nothing is translated in either mode, and the b letter in the mode string has no effect. The last two lines of the output above confirm it: the string "A\nB\n" is 4 characters, and both modes produced a 4-byte file.
On a Windows C runtime the "w" line would report 6 bytes. That difference is why you should still write "b" for binary data: it keeps your code correct if it is ever built for another platform.
How the end of a file is detected
The file system records the length of every file, text or binary, and a read function reports end-of-file when that length is reached. Some old DOS-era text-mode implementations also treated the byte with value 26 (Ctrl+Z) as an end marker. That is a legacy rule and does not apply on Linux. At a Linux terminal, pressing Ctrl+D tells the terminal driver that input is finished; it is not stored as a character in a file.
while (!feof(fp)) as your main test. feof becomes true only after a read has already failed, so the loop runs once too often. Test the return value of the read function itself, for example fgets(...) != NULL.
Text File vs Binary File in C: Speed, Size and Portability
| Question | Text file | Binary file |
|---|---|---|
| Which is faster? | Slower, because numbers are converted to and from characters | Faster, bytes are copied as they are |
| Which is smaller? | Depends on the numbers; large values cost more bytes | Always the type size, predictable |
| Which is easier to debug? | Open it with cat or an editor | Needs od, hexdump or a viewer |
| Which survives a change of CPU? | Yes | Only if size, padding and endianness match |
Real-world use
Configuration files, CSV logs and command scripts are text because people read and edit them. Firmware images, sensor sample streams, audio and images are binary because speed and exact size matter. Many projects keep both: a compact binary log for the device and a text export for the engineer.
Common Mistakes with a Text File vs Binary File in C
- Opening a binary file with
"r"on Windows, which corrupts data through newline translation. Use"rb". - Assuming
sizeof(int)is 2 or 4 everywhere. Useint16_t,uint32_tand friends. - Writing a whole
structwithfwritewithout remembering that padding bytes and byte order can differ between compilers and CPUs. - Reading text with
freadand forgetting the buffer needs a terminating'\0'. - Comparing file size with the number of digits you expect, without counting the newline separator.
Best Practices
- In any text file vs binary file in C decision, pick text for anything a human edits and binary for bulk data.
- Always pass
"b"for binary files, even on Linux. - Document the record layout: field order, sizes, byte order.
- Put a small header (magic number and version) at the start of binary formats so old files can be recognised.
- Inspect files with
od -An -tx1 filewhile developing.
Key Takeaways
- In a text file vs binary file in C, text stores characters and binary stores memory bytes.
- 1679 is four bytes as text and two bytes as a 16-bit binary value (four as a 32-bit one).
- Newline translation happens only in text mode on systems like Windows; Linux never translates.
- Binary data is fast but tied to type sizes and byte order.
You can now choose between a text file vs binary file in C for any project. Next in this free embedded C course we study the buffer that sits between your program and the disk, and why data you wrote may not be in the file yet.
Frequently Asked Questions
What is the difference in a text file vs binary file in C?
A text file stores characters and line breaks and is readable in an editor. A binary file stores raw bytes exactly as they appear in memory and needs a program that understands its layout.
Why does 1679 take 4 bytes in a text file?
Because each of its four digits is stored as one character byte: ‘1’, ‘6’, ‘7’ and ‘9’.
Does Linux treat text and binary files differently?
No. On Linux the kernel sees only bytes, and the b in the mode string is ignored. The distinction matters for portability to systems that translate newlines.
What does little-endian mean?
The least significant byte of a multi-byte value is stored first. 0x068F is written as 8F 06 on a little-endian machine.
Which is faster, text or binary file I/O?
Binary, because no conversion between numbers and characters is needed.
Is the Ctrl+Z end-of-file character used on Linux?
No. It is a legacy DOS and Windows text-mode convention. Linux relies on the file length, and Ctrl+D only signals end of input at a terminal.
Should I use binary files in embedded C programming?
Yes for bulk or timing-sensitive data, as long as you fix the type sizes and byte order. Use text for settings and logs people must read.
Continue the free C tutorial
Next lecture: file buffers, flushing and why your data sometimes seems to be missing.

2 Comments