How it works...

The first file name that is passed through the command-line arguments is opened in read-only mode. The second file name that is passed through the command-line arguments is opened in write-only mode. If both files are opened correctly, then the fp and fq pointers , respectively, will point at the read-only and write-only files.

We will set a while loop to execute until it reaches the end of the source file. Within the loop, one line from the source file will be read using the fgets function. The fgets function reads the specified number of bytes from the file or until the new line character, \n, is reached. If the new line character does not appear in the file, then the BUFFSIZE constant limits the bytes to be read from the file to 254.

The line read from the file is assigned to the buffer string . The length of the string buffer is computed and assigned to the variable, n. We will then set a for loop to execute until it reaches the end of the length of the buffer string, and within the loop, the ASCII value of each character will be changed.

To encrypt the file, we will subtract a value of 45 from the ASCII value of each of the characters, although we can apply any formula we like. Just ensure that you remember the formula, as we will need to reverse it in order to decrypt the file.

After applying the formula to all of the characters, the encrypted line will be written into the target file. In addition, to display the encrypted version on the screen, the encrypted line will be displayed on the screen.

When the while loop is finished, all of the lines from the source file will be written into the target file after they are encrypted. Finally, the two files will be closed.

Let's use GCC to compile the encryptfile.c program, as follows:

D:\CBook>gcc encryptfile.c -o encryptfile

If you get no errors or warnings, this means that the encryptfile.c program  has been compiled into an executable file, encryptfile.exe. Let's run this executable file.

Before running the executable file, though, let's take a look at the text file, textfile.txt, which will be encrypted using this program. The contents of this text file are as follows:

I am trying to create a sequential file. it is through C programming. It is very hot today. I have a cat. do you like animals? It might rain. Thank you. bye

Let's run the executable file, encryptfile.exe, on textfile.txt and put the encrypted content into another file named encrypted.txt using the following code:

D:\CBook>./encryptfile textfile.txt encrypted.txt

The normal content in textfile.txt is encrypted and the encrypted content is written into another file named encrypted.txt. The encrypted content will appear as follows:

D:\CBook>type encrypted.txt
≤4@≤GEL<A:≤GB≤6E84G8≤4≤F8DH8AG<4?≤9<?8≤<G≤<F≤G;EBH:;≤≤CEB:E4@@<A:≤≤≤G≤<F≤I8EL≤;BG≤GB74L≤;4I8≤4≤64G≤≤7B≤LBH≤?<>8≤4A<@4?F≤≤≤≤G≤@<:;G≤E4<A';4A>≤LBH≤5L8
The preceding command is executed in Windows' Command Prompt.

Voila! We've successfully encrypted the file!