Module 5 Files, Macros
Module 5 Files, Macros
Data files allow us to store information permanently, and to access and alter that
information whenever necessary.
The second statement defines a pointer called fpt which will point to a structure of
type FILE. indicating the beginning of the data-file buffer area.
The third statement opens a new data file called sample. dat as a write-only file
fpt points to the buffer area associated with the data file sample. Dat
return 0;
}
to create the file in a specific folder, just provide an absolute path:
count is the number of elements to be written, size is the size of each element to be
written, and the file pointer is a pointer to the file where the data will be written.
Syntax for using the fread() method with unformatted input:
a pointer to the buffer where the data will be read, the size of each element to be
read, the number of elements to be read, and a pointer to the file from which the
data will be read.
The library functions fread and fwrite are intended to be used in situations of this
type.
These functions are often referred to as unformatted read and write functions.
Similarly, data files of this type are often referred to as unformatted data files.
where customer is a structure variable of type record, and fpt is the stream pointer
associated with a data file that has been opened for output
Concept of Binary File
A binary file contains data in binary form (i.e. 0’s and 1’s) instead of ASCII characters.
They contain data that is stored in a similar manner to how it is stored in the main
memory.
● The binary files can be created only from within a program and their contents
can only be read by a program.
● More secure as they are not easily readable.
● They are generally stored with .bin file extension.
All machine language files are binary files.
For opening a binary files, filemode has to be mentioned as “rb” or “wb” in fopen
command.
b stands for binary(files)
Binary files differ from text files in two ways:
The storage of new line characters
The eof character
Differences between Text and Binary file
■ A text file stores data in the form of alphabets, digits and other special symbols by
storing their ASCII values and are in a human readable format. For example, any file
with a .txt, .c, etc extension. Whereas, a binary file contains a sequence or a
collection of bytes which are not in a human readable format. For example, files with
.exe, .mp3, etc extension. It represents custom data.
■ A small error in a textual file can be recognized and eliminated when seen.
Whereas, a small error in a binary file corrupts the file and is not easy to detect.
■ Since the data is not human readable it also adds to the security of the content as
one might not be able to get data if the structure is not known.
■ Now, when it comes to programming the major differences between the two, are.,
Handling of newlines, representation of EOF(End of File).
text mode and the binary mode can be distinguished is on the basis of the
representation of the end-of-file(EOF). In the text mode, a special character with the
ASCII code 26 is inserted at the end of the file. This character when encountered
returns the EOF signal to the program.
This is not the case in binary mode. In the binary mode, we do not have any special
character to signify the EOF. It keeps track with the help of the number of characters
present in the directory entry of the file.
Opening a binary file
To open a file in binary mode, use the rb, rb+, ab, ab+, wb, and wb+ access
mode in the fopen() function. Also use the .bin file extension in the binary
filename.
Example
fptr = fopen("filename.bin", "rb");
Write to a Binary File
Use fwrite() function to write data to a binary file. The data is written to the binary file in the from of bits
(0’s and 1’s).
Syntax of fwrite()
size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *file_pointer);
Parameters:
● ptr: pointer to the block of memory to be written.
● size: size of each element to be written (in bytes).
● nmemb: number of elements.
● file_pointer: FILE pointer to the output file stream.
// C program to write to a Binary file using fwrite()
#include <stdio.h>
#include <stdlib.h>
struct threeNum {
int n1, n2, n3;
};
int main()
{
int n;
// Structure variable declared here.
struct threeNum num;
FILE* fptr;
if ((fptr = fopen("C:\\program.bin", "wb")) == NULL) {
printf("Error! opening file");
// If file pointer will return NULL
// Program will exit.
exit(1);
}
int flag = 0;
// else it will return a pointer to the file.
for (n = 1; n < 5; ++n) {
num.n1 = n;
num.n2 = 5 * n;
num.n3 = 5 * n + 1;
flag = fwrite(&num, sizeof(struct threeNum), 1,
fptr);
}
fclose(fptr);
return 0;
}
REGISTER VARIABLES
Registers are special storage areas within the computer’s central processing unit.
The actual arithmetic and logical operations that comprise a program are carried
out within these registers.
Normally, these operations are carried out by transferring information from the
computer’s memory to these registers, carrying out the indicated operations, and
then transferring the results back to the computer’s memory.
The execution time can be reduced considerably if certain values can be stored
within these registers rather than in the computer’s memory.
Such programs may also be somewhat smaller in size (i.e., they may require
fewer instructions), since fewer data transfers will be required.
In C, the values of register variables are stored within the registers of the central
processing unit.
A variable can be assigned this storage class simply by preceding the type
declaration with the keyword register.
register int a, b, c;
This declaration specifies that the variables a, b and c will be integer variables with
storage class register.
Hence, the values of a, b and c will be stored within the registers of the computer’s
central processing unit rather than in memory, provided the register space is
available.
If the register space is not available, then the variables will be treated as integer
variables with storage class automatic. This is equivalent to the declaration auto
int a, b, c;
C program to implement register variable
#include <stdio.h>
int main() {
register char x = 'B';
register int a = 20;
register short b = 35;
printf("The value of register variable x : %c\n",x);
printf("The value of register variable a : %d\n",a);
printf("The value of register variable b : %d\n",b);
return 0;
}
BITWISE OPERATIONS
Some applications require the manipulation of individual bits within a word of
memory.
These bitwise operators can be divided into three general categories: the one’s
complement operator, the logical bitwise operators, and the shift operators.
The One’s Complement Operator
The one’s complement operator (-) is a unary operator that causes the bits of its
operand to be inverted (i.e., reversed), so that 1s become OS and OS become 1s.
The operand must be an integer-type quantity (including integer, long, short,
unsigned, char, etc.).
Consider the hexadecimal number 07ff. The corresponding bit pattern, expressed
in terms of a 16-bit word, is 0000 01 11 11 11 11 11 .
The one’s complement of this bit pattern is 11 11 1000 0000 0000, which
corresponds to the hexadecimal number f8OO.
Thus, the value of the expression - 07ff is f800.
The Logical Bitwise Operators
the least significant bits (i.e., the rightmost bits) within the two operands will
be compared, then the next least significant bits, and so on, until all of the bits
have been compared.
A bitwise and expression will return a 1 if both bits have a value of 1 (i.e.,
if both bits are true). Otherwise, it will return a value of 0.
A bitwise exclusive or expression will return a 1 if one of the bits has a
value of 1 and the other has a value of 0 (one bit is true, the other false.
Otherwise, it will return a value of 0.
A bitwise or expression will return a 1 if one or more of the bits have a
value of 1 (one or both bits are true). Otherwise, it will return a value of 0.
Masking
int main()
{
// creating today variable of enum week type
enum week today;
today = Wednesday;
printf("Day %d",today+1);
return 0;
}
MACROs
// Driver Code
int main()
{
printf("length =" );
scanf("%d",&length);
printf("width =" );
scanf("%d",&width);
printf("\n area = %d",area);
return 0;
}
#include <stdio.h>
#define ADD(x,y) x+y
int main()
{
printf("%d", ADD(3,2));
return 0;
}