Text and Binary Streams in C

What is a Stream in C?

A stream is simply a flow of data. In C, all input and output operations are done through streams. You can think of it as a path through which data moves between your program and a file, keyboard, or screen.

There are two main types of streams in C:

  • Text Stream
  • Binary Stream

Text Streams in C

A text stream is a sequence of characters. It translates certain characters during input and output. For example:

  • Newline characters (\n) in Unix or Linux get converted to \r\n in Windows.
  • End-of-file (EOF) markers might be handled specially.

Key Features:

  • Data is stored as human-readable characters.
  • Text mode handles newline conversion depending on the system.
  • Useful for editing files manually.

Opening Files in Text Mode:

Use fopen() function with one of these modes:

advertisement
fopen("filename.txt", "r");  // read mode
fopen("filename.txt", "w");  // write mode
fopen("filename.txt", "a");  // append mode

Example: Writing to a Text File

#include <stdio.h>
 
int main()
{
    // Open a text file in write mode
    FILE *quiz = fopen("quiz.txt", "w");
 
    if (quiz == NULL)
   {
        perror("Failed to open file");
        return 1;
    }
 
    fputs("Welcome to Sanfoundry Certification!\n", quiz);
    fputs("Your score is 95/100\n", quiz);
 
    fclose(quiz);  // Always close the file
    return 0;
}

Binary Streams in C

A binary stream is a sequence of bytes. No translation is done — data is read and written exactly as it is.

Free 30-Day Python Certification Bootcamp is Live. Join Now!

Key Features:

  • Data is stored in raw byte format.
  • No newline or EOF conversion.
  • Best for storing structured data (e.g., structs, images).

Opening Files in Binary Mode:

Use fopen() with b in the mode:

fopen("filename.bin", "rb");  // read in binary
fopen("filename.bin", "wb");  // write in binary
fopen("filename.bin", "ab");  // append in binary

Example – Writing to a Binary File

#include <stdio.h>
#include <stdlib.h>
 
int main()
{
    // Binary write
    FILE *certification = fopen("certification.dat", "wb");  
 
    if (certification == NULL) {
        perror("File open failed");
        return 1;
    }
 
    int scores[] = {90, 85, 88, 92};
    // Write binary data
    fwrite(scores, sizeof(int), 4, certification);  
 
    fclose(certification);
    return 0;
}

Comparison: Text vs Binary Streams

Aspect Text Streams Binary Streams
Data Representation Human-readable characters Raw byte sequences
Line Endings Platform-dependent (\n, \r\n) No special treatment
File Size Larger due to character encoding Smaller, more efficient
Read/Write Functions fgets, fprintf, fscanf fread, fwrite
Use Cases Logs, configuration files, source code Images, audio files, executables

Best Practices

  • Mode Selection: Always open files in the appropriate mode (“r”/”w” for text, “rb”/”wb” for binary).
  • Error Checking: After opening a file, check if the file pointer is NULL to ensure the file was opened successfully.
  • Resource Management: Always close files using fclose() to free system resources.
  • Portability: Be cautious of platform-specific behaviors, especially regarding line endings in text files.
  • Data Integrity: When dealing with binary files, ensure that the data structures are consistent across different systems to avoid issues with endianness or padding.

Sanfoundry Global Education & Learning Series – 1000 C Tutorials.

advertisement
If you wish to look at all C Tutorials, go to C Tutorials.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.