In modern terms we call binary files a stream of bytes and more modern languages tend to work with streams rather than files. The important part is the data stream rather than where it came from! In C you can think about the data either in terms of files or streams. Or if it helps, think of a file/stream as a very long array! With random access you can read or write to any part of this array. With sequential you have to loop through it from the start like a big tape.
Download Example 1.
This shows a simple binary file being opened for writing, with a text string (char *) being written into it. Normally you'd use a text file for that but I wanted to show that you can write text to a binary file.
// ex1.c
#include <stdio.h>
#include <string.h>
int main(int argc, char * argv[])
{
const char * filename="test.txt";
const char * mytext="Once upon a time there were three bears.";
int byteswritten=0;
FILE * ft= fopen(filename, "wb") ;
if (ft) {
fwrite(mytext,sizeof(char),strlen(mytext), ft) ;
fclose( ft ) ;
}
printf("len of mytext = %i ",strlen(mytext)) ;
return 0;
}
On the next page : How Example 1 Works

