UNIT: 5 MACROS AND FILE PROCESSING
Preprocessor Directives: Introduction to preprocessor directives in Simple macros using `#define`, conditional
macros using `#ifdef`, `#ifndef`, `#endif`, `#else`, `#elif`.
Files: Introduction to Files – Opening a file – Reading Data from Files – Writing Data to Files –
Detecting the End-of-file –Closing a file – Sequential access file-Random Access Files – Binary Files –
Command line arguments.
Preprocessor directives are commands in C and C++ that instruct the compiler to
preprocess the source code before actual compilation begins. They begin with a `#`
symbol and are processed by the preprocessor, which is a part of the compiler.
### Simple Macros using `#define`
Macros defined using `#define` are simple text replacements performed by the
preprocessor. They are typically used to define constants or simple functions.
```c
#include <stdio.h>
#define PI 3.14159
#define SQUARE(x) ((x) * (x))
int main() {
double radius = 2.0;
double area = PI * SQUARE(radius);
printf("Area of circle: %f\n", area);
return 0;
}
```
In this example:
- `PI` is a constant macro for the value of pi.
- `SQUARE(x)` is a macro that computes the square of `x`.
### Conditional Macros using `#ifdef`, `#ifndef`, `#endif`, `#else`, `#elif`
Conditional directives allow parts of the code to be included or excluded based on
conditions evaluated by the preprocessor.
```c
#include <stdio.h>
#define DEBUG 1
int main() {
#ifdef DEBUG
printf("Debugging information\n");
#else
printf("No debugging information\n");
#endif
return 0;
}
```
In this example:
- `#ifdef DEBUG` checks if the macro `DEBUG` is defined. If it is defined (`#ifdef`),
the code block between `#ifdef` and `#else` (or `#endif`) is included in the compiled
code.
- `#else` specifies what to do if `DEBUG` is not defined.
- `#endif` marks the end of the conditional directive block.
You can also use `#ifndef` to check if a macro is not defined:
```c
#ifndef DEBUG
printf("No debugging information\n");
#else
printf("Debugging information\n");
#endif
```
Additionally, `#elif` can be used to provide an "else if" condition within a sequence of
`#ifdef` or `#ifndef` checks:
```c
#if defined(DEBUG_LEVEL) && DEBUG_LEVEL == 2
printf("Debugging level 2 information\n");
#elif defined(DEBUG_LEVEL) && DEBUG_LEVEL == 1
printf("Debugging level 1 information\n");
#else
printf("No debugging information\n");
#endif
```
### Summary
- **`#define`**: Defines macros for simple text substitution.
- **`#ifdef`, `#ifndef`**: Conditionally includes code based on whether a macro is
defined or not.
- **`#endif`**: Ends a conditional directive block.
- **`#else`**: Specifies alternative code to be included if the condition in `#ifdef` or
`#ifndef` is false.
- **`#elif`**: Provides an "else if" condition within a sequence of conditional checks.
These preprocessor directives provide flexibility and control over how the source
code is processed and compiled, allowing for conditional compilation and defining
macros for code simplification and abstraction.
File handling
File: the file is a permanent storage medium in which we can store the data
permanently.
Types of file can be handled
we can handle three type of file as
(1) sequential file
(2) random access file
(3) binary file
File Operation
opening a file:
Before performing any type of operation, a file must be opened and for this
fopen() function is used.
syntax:
file-pointer=fopen(“FILE NAME ”,”Mode of open”);
example:
FILE *fp=fopen(“ar.c”,”r”);
If fopen() unable to open a file than it will return NULL to the file pointer.
File-pointer: The file pointer is a pointer variable which can be store the address
of a special file that means it is based upon the file pointer a file gets opened.
Declaration of a file pointer:-
FILE* var;
Modes of open
The file can be open in three different ways as
read mode ’ r’/rt
Write mode ’w’/wt
Appened Mode ’a’/at
Reading a character from a file
getc() is used to read a character into a file
Syntax:
character_variable=getc(file_ptr);
Writing acharacter into a file
putc() is used to write a character into a file
puts(character-var,file-ptr);
CLOSING A FILE
fclose() function close a
file. fclose(file-ptr);
fcloseall () is used to close all the opened file at a time
File Operation
The following file operation carried out the file
(1)creation of a new file
(3)writing a file
(4)closing a file
Before performing any type of operation we must have to open the file.c, language
communicate with file using A new type called file pointer.
Operation with fopen()
File pointer=fopen(“FILE NAME”,”mode of open”);
If fopen() unable to open a file then it will return NULL to the file-pointer.
Reading and writing a characters from/to a file
fgetc() is used for reading a character from the file
Syntax:
character variable= fgetc(file pointer);
fputc() is used to writing a character to a file
Syntax:
fputc(character,file_pointer);
/*Program to copy a file to
another*/ #include<stdio.h>
void main()
{
FILE *fs,*fd;
char ch;
If(fs=fopen(“[Link]”,”r”)==0)
printf(“sorry….The source file cannot be opened”);
return;
}
If(fd=fopen(“[Link]”,”w”)==0)
printf(“Sorry…..The destination file cannot be opened”);
fclose(fs);
return;
}
while(ch=fgets(fs)!=EOF)
fputc(ch,fd);
fcloseall();
Reading and writing a string from/to a file
getw() is used for reading a string from the file
Syntax:
gets(file pointer);
putw() is used to writing a character to a file
Syntax:
fputs(integer,file_pointer);
#include<stdio.h>
#include<stdlib.h>
void main()
FILE *fp;
int word;
/*place the word in a file*/
fp=fopen(“[Link]”,”wb”);
If(fp==NULL)
{
printf(“Error opening file”);
exit(1);
}
word=94;
putw(word,fp);
If(ferror(fp))
printf(“Error writing to file\n”);
else
printf(“Successful write\n”);
fclose(fp);
/*reopen the file*/
fp=fopen(“[Link]”,”rb”);
If(fp==NULL)
{
printf(“Error opening file”);
exit(1);
}
/*extract the word*/
word=getw(fp);
If(ferror(fp))
printf(“Error reading file\n”);
else
printf(“Successful read:word=%d\n”,word);
/*clean up*/
fclose(fp);
}
Reading and writing a string from/to a file
fgets() is used for reading a string from the file
Syntax:
fgets(string, length, file pointer);
fputs() is used to writing a character to a file
Syntax:
fputs(string,file_pointer);
#include<string.h>
#include<stdio.h>
void main(void)
{
FILE*stream;
char string[]=”This is a test”;
char msg[20];
/*open a file for update*/
stream=fopen(“[Link]”,”w+”);
/*write a string into the file*/
fwrite(string,strlen(string),1,stream);
/*seek to the start of the file*/
fseek(stream,0,SEEK_SET);
/*read a string from the file*/
fgets(msg,strlen(string)+1,stream);
/*display the string*/
printf(“%s”,msg);
fclose(stream);
}