0% found this document useful (0 votes)
12 views

Unit - V C Programming

Uploaded by

bareenafifa
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Unit - V C Programming

Uploaded by

bareenafifa
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Unit - V

FILE
Pre processor
C preprocessor is a micro processor that is used by #if
compiler to transform your code before
compilation. It is called micro preprocessor #else
because it allows us to add macros.
#elif
All preprocessor directives starts with hash #
symbol. #endif
list of preprocessor directives. #error
#include
#pragma
#define
#undef
#ifdef
#ifndef
Macros
A macro is a segment of code which is replaced by the value of macro.
Macro is defined by #define directive. There are two types of macros:
Object-like Macros
Function-like Macros
Object-like Macros
The object-like macro is an identifier that is replaced by value. It is
widely used to represent numeric constants. For example:
#define PI 3.14
Here, PI is the macro name which will be replaced by the value 3.14.
Function-like Macros
The function-like macro looks like function call. For example:
#define MIN(a,b) ((a)<(b)?(a):(b))
Here, MIN is the macro name.
C Predefined Macros
ANSI C defines many predefined macros that can be used in c program.

No. Macro Description

1 _DATE_ represents current


date in "MMM DD
YYYY" format.
2 _TIME_ represents current
time in "HH:MM:SS"
format.
3 _FILE_ represents current file
name.
4 _LINE_ represents current line
number.
5 _STDC_ It is defined as 1 when
compiler complies with
the ANSI standard.
File
A file is a container in computer storage devices used for storing data.
Why files are needed?

When a program is terminated, the entire data is lost. Storing in a file


will preserve your data even if the program terminates.
If you have to enter a large number of data, it will take a lot of time to
enter them all.
However, if you have a file containing all the data, you can easily access
the contents of the file using a few commands in C.
You can easily move your data from one computer to another without
any changes.
Types of Files

When dealing with files, there are two types of files you should know
about:
Text files
Binary files
1. Text files ( Stream oriented data)
Text files are the normal .txt files. You can easily create text files using
any simple text editors such as Notepad.
When you open those files, you'll see all the contents within the file as
plain text. You can easily edit or delete the contents.
2. Binary files (system
oriented data)
Binary files are mostly the .bin files in your computer.
Instead of storing data in plain text, they store it in the binary form (0's
and 1's).
The following operations
can be performed on a
file.
Creation of a new file (fopen with attributes as “a” or “a+” or “w” or
“w++”)
Opening an existing file (fopen)
Reading from file (fscanf or fgets)
Writing to a file (fprintf or fputs)
Moving to a specific location in a file (fseek, rewind)
Closing a file (fclose)
Functions for file
handling
No. Function Description

1 fopen() opens new or existing


file
2 fprintf() write data into the file
3 fscanf() reads data from the
file
4 fputc() writes a character into
the file
5 fgetc() reads a character from
file
6 fclose() closes the file
7 fseek() sets the file pointer to
given position
8 fputw() writes an integer to
file
9 fgetw() reads an integer from
file
10 ftell() returns current
position
11 rewind() sets the file pointer to
the beginning of the
Mode
f=fopen(filename,mode);
Mode Description

r opens a text file in read mode


w opens a text file in write mode
a opens a text file in append mode
r+ opens a text file in read and write mode
w+ opens a text file in read and write mode
a+ opens a text file in read and write mode
rb opens a binary file in read mode
wb opens a binary file in write mode
ab opens a binary file in append mode
rb+ opens a binary file in read and write
mode
wb+ opens a binary file in read and write
mode
ab+ opens a binary file in read and write
mode
declare a file.
FILE *fp;
Input/Output operation on File
In the above table we have discussed about various file I/O functions to
perform reading and writing on file. getc() and putc() are the simplest
functions which can be used to read and write individual characters to a
file.
#include<stdio.h> fclose(fp);
fp = fopen("one.txt", "r");
int main()
{ while( (ch = getc(fp)! = EOF)
FILE *fp; printf("%c",ch);
char ch;
fp = fopen("one.txt", "w"); // closing the file pointer
printf("Enter data..."); fclose(fp);
while( (ch = getchar()) != EOF) {
putc(ch, fp); return 0;
} }
Thank you..

You might also like