Why You Cannot Watch Videos With your Word Processor

After reading one of the most hilarious answers on Quora about some examples of software companies who ran out of business because of too many bugs greatest example of software failure. One of the greatest failures was that of the infamous WordStar word processing software which was anticipated as the tool to escort word processing into the 2000s golden era. Sadly, this assumption didn't see beyond the hopes accrued to it. This short remark will make more sense as you proceed into this article. The following are discussed in this article:

  • What is a File
  • Types of File formats we have
  • What formats you can write a file
  • What File Pointers are and how you can create magic with them
  • Differences between writing a file as binary or a text file
  • Practical examples of file writing as binary and text
  • A project that exploits what has been covered.

Let's kick the ball rolling !!!!!

My first day as a student in his sophomore year was meted with a mixture of anticipation and stress. We had to carry this brown paper that contained all our details inside it to both the senate and faculty building which are like two light-years apart(this pun is intended). On submitting my file, the officer in charge of the scrutiny requested the details in a certain order and I began to ask myself why? My inquisitiveness was further enfired because I see no reason to roast my skin under the AC; because the highly esteemed sought details in my file can all be pulled from the school's portal by these officers. Well, they said it's for tradition and backup sake.

tadry.jpg Now, there are some critical things in this story, first is that a brown thing is involved. Secondly, certain pieces of data(details) are contained in the file. Lastly, the information was requested to be stored in a particular order. A file in a computer system is like a brown container which is independent of the computer system, it can be imported, exported; or even sold!!!! that's the highest highness it can assume. In computer terms, a file is a serial sequence of bytes.

If not for files, the scope of your computer would have been limited to what is stored in its main memory.

Imagine having an ATM system that requires you to input every of your personal detail before you can perform your desired transactions on it!!! That'll be too cruel and time-consuming right. Those pieces of data that you do not supply while using your ATM have been saved in a file for you and the computer fetches them through some commands.

commands.jpg

Types of Files and their corresponding Formats

An exhaustive list of file types and their formats are available here types of files

What are File Pointers

File Pointers are like mechanisms which help the computer to monitor the movements in a file. Imagine having a file which contains your name, address, account balance and age. How do you instruct the computer to fetch just the age for you? This is made possible through File pointers. They help the computer measure three basic positions in a file: Start, Current and End position. With these three positions, you can manipulate a file anyhow you want.

filestructure.png

How Files are Written and Read

There are two major ways of writing data to a File and reading data from a File. You can either write or read a file as :

  1. Text File

  2. Binary File

Have you ever tried opening a jpeg File with a word processing tool? What do you see when you look at it? Jargons right!!! things that only aliens can make sense of right.

gibb.png Now, let's treat writing and reading a File as a Text.

When you write a File as a Text file, this means that every information inside the File is stored as a character. if you try to save a number inside the file, the number will be converted into its character equivalent and life goes on.

In summary, writing and reading a file as a text entails storing your data as characters and reading them back as characters too. Most text editors use this weapon.

Writing a File as Binary

Binary files store information without any implicit conversion of such information. A number is always stored as a number in a binary file, a character is always stored as a character and anything is stored as anything. No manipulations are involved!!!!.

Differences between writing a file as either Binary or Text

Reading a text-written File does not require extra knowledge about the sequence in which the File is stored since everything in the File is a character.

However, binary files are complicated because they require knowledge of the sequence of the data stored in the Files. If you don't understand the term data types yet, I have a beginner-friendly article on it beginner-friendly intro to data types.

The one who intends to read this data must know the sequence of storage of this data. For example, assuming the first thing stored in the file is an integer, then followed by a float and finally a string of characters. Trying to read the File as a string first, int second and float third will result in the gibberish things you see when you try to read an image file as a text file.

surprise.jpg Writing a File in binary finds its use in security as the encoding or sequence of storage into such Files is only known by the one who stored the data into such Files. Hence, highly classified data are stored using this File storage technique.

When it comes to memory optimization, storing a File as binary outshines storing it as Text because there's no implicit conversion to characters involved in binary storage of Files. Factually, storing a File as binary saves a lot of memory, for example, storing the integer - 24567897 in a text file takes 8 bytes of memory while storing it as a binary file takes only 4 bytes of memory; wow that's some huge leverage in hardware programming!!!!

happy.jpg

Some Practical Examples of writing a file as Binary and Text

1. Writing and Reading in Binary

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char const *argv[])
{
    FILE *fp = NULL;               // setting the file pointer to null
    int m = 14;                    // Integer value to be written to file
    int j = 0;                     // a place holder variable
    fp = fopen("file.bin", "wb+"); // opening file in binary mode for both writing and reading
    while (!fp)
    {
        fprintf(stderr, "Unable to open file for writing"); // error message to show that file opening for writing was unsuccessful
        exit(1);
    }
    fwrite(&m, sizeof(int), m, fp); // writing into the file named file.bin

    fflush(fp);  // flushing standard input 
    rewind(fp);  // rewinding file pointer 
    fread(&j, sizeof(int), 1, fp); //reading from the file into the place holder variable j
    printf("%d",j); //printing program output to stdout
    fclose(fp); // closing of file 
    return 0;
}
2. Writing and Reading in Text

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char const *argv[])
{
    FILE *fp = NULL;               // setting the file pointer to null
    int m = 14;                    // Integer value to be written to file
    int j = 0;                     // a place holder variable
    fp = fopen("file.txt", "w+"); // opening file in binary mode for both writing and reading notice the absence of the suffix "b"
    while (!fp)
    {
        fprintf(stderr, "Unable to open file for writing"); // error message to show that file opening for writing was unsuccessful
        exit(1);
    }
    fprintf(fp,"%d", m); // writing into the file named file.bin

    fflush(fp);  // flushing standard input 
    rewind(fp);  // rewinding file pointer 
    fscanf(fp, %d,&j); //reading from the file into the place holder variable j
    printf("%d",j); //printing program output to stdout
    fclose(fp); // closing of file 
    return 0;
}

Some Projects that exploit these File storage methods

I implemented a project titled ATM project. This project imitates the behaviour of a real-time ATM. The project can be found on my Github repository ATM Project. Another real-world implementation is a basic login system.

The only limit here is your imagination because you are just one amongst the thousand programmers who are file storage literate and can turn the world around with this knowledge. You should be able to guess some of the causes of the bugs in the WordStar software after journeying this deep into this article. You can comment your guesses under this article. Kindly like this article and follow me for more knowledge-savvy content like this. Thanks for reading.