0% found this document useful (0 votes)
31 views6 pages

PPS Chapter Files

The document provides an overview of file handling in C, including types of files (text and binary), file operations (creating, opening, reading, writing, and closing files), and associated functions. It also covers error handling during file operations and introduces preprocessor directives used in C programming. Key functions such as fopen(), fclose(), fputc(), and fscanf() are detailed for performing various file I/O tasks.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views6 pages

PPS Chapter Files

The document provides an overview of file handling in C, including types of files (text and binary), file operations (creating, opening, reading, writing, and closing files), and associated functions. It also covers error handling during file operations and introduces preprocessor directives used in C programming. Key functions such as fopen(), fclose(), fputc(), and fscanf() are detailed for performing various file I/O tasks.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

UNIT-V

Files: Introduction to files, file operations, reading data from files, writing data to files, error handing
during file operations.
Preprocessor Directives: Types of preprocessor directives

File Handling
INTRODUCTION :

In real world application when data is large then it is necessary to stored on the disk in files. A file
represents a sequence of bytes. file is created in secondary storage. Files are of two types text file and
binary file.

C language provides access on files with high/low level functions .FILE is a structure defined in
standard input/output library. file pointer is created and used as

FILE *fp; // fp is a file pointer

There are two types of files

Text File : A text stream consist of a sequence of characters. they are grouped in line. stores the data in
the text form

Binary File : stores the data in the binary form (1's and 0's). a binary stream consist of a sequence of
data. they stored in their memory representation.

In C language support a number of functions to perform basic operation on file

 creating a file
 opening a file
 writing into the file
 reading from the file
 close the file

files I/O functions are :

Operations Description
fopen() create a file or open an existing file
fclose() close a file
getc() read a character
putc() write a character
fprintf() write a formatted values
fscanf() read set of values
getw() read an integer
putw() write an integer

DEFINING AND OPENING AND CLOSING A FILE

The fopen(filename,mode) function to create a new file or to open an existing file.


fopen function is used to open a file . the first argument is file name and second argument is access
mode.
access mode can have
r read mode
w writing mode
a append mode
r+ reading and writing
w+ reading and writing and content is truncated
a+ reading and writing

FILE *fp; // file pointer


fp=fopen("abc.txt","r"); // open a file

After performing all operation on the file must be closed at the end
fclose()is used for closing the file
The fclose() function returns zero on success, or EOF if there is an error in closing the file
FILE *fp; // file pointer
fclose(fp); // at the end close the file

Example : Opening a File


#include<stdio.h>
int main()
{
FILE *fp;
fp = fopen(“abc.txt”,”w”);
}
Closing a file :

FILE *fp;
fp = fopen(“abc.txt”,”r”);

fclose(fp);
INPUT/OUTPUT OPERATIONS ON FILES

Writing into the file

fputc() is used to write a character ch in the file pointed by fp. it returns the character on success
otherwise EOF.

Syntax : int fputc(int ch,FILE *fp);

fputs(char *s,FILE *fp) is used to write a string into the file. it returns the negative value on success
otherwise EOF.

Syntax : int fputs(char *s,FILE *fp);

fprintf(FILE *fp ,"format ",...) is similar to printf function but it prints the formated data into the file
Syntax :
putw() is used for writing a integer to a file
Syntax :
fwrite(void *p, int size, int no, FILE *fp) writes a block of data into the file reading from the file
Syntax :
int fgetc(FILE *fp) is used to read a charecter from the file otherwise EOF on end of the file
Syntax :
char *fgets(char *str, int n,FILE *fp) is used to read a string from the file
Syntax :
fscanf(FILE *fp,"format", ..) it is similar to scanf function but it is used to read from the file
Syntax :
getw() to read a integer from a file
Syntax :
fread(void *ptr, int size,int no,FILE *fp) reads a block of data from the file
Syntax :

Example : Write to a text file

#include <stdio.h>

int main() {

int i;

FILE * fptr;
char fn[50];

char str[] = "I like c programming \n";

fptr = fopen("fputc_test.txt", "w"); // "w" defines "writing mode"

for (i = 0; str[i] != '\n'; i++) {

/* write to file using fputc() function */

fputc(str[i], fptr);

fclose(fptr);

return 0;

2. Read from a text file


#include <stdio.h>
#include <stdlib.h>
.
int main()
{
. int num;
FILE *fptr;

if ((fptr = fopen("C:\\program.txt","r")) == NULL){


printf("Error! opening file");

// Program exits if the file pointer returns NULL.


exit(1);
}

fscanf(fptr,"%d", &num);

printf("Value of n=%d", num);


fclose(fptr);

return 0;
. }
RANDOM ACCESS FILE OPERATION

fseek() is used to file pointer to different positions.


ftell() is used to find current position of file pointer
rewind() to get file pointer a the begining of the file

ERROR HANDLING FILES

File status functions:


the functions used to handle file status are:
feof() test end of file
ferror() test error
clearerr() clear error

feof() it is used to check if end of the file has been reached

syntax: int feof(FILE *fp);

ferror() it is used to check the error status of the file. it returns true if it finds error, otherwise false.

syntax: int ferror(FILE *fp);

clearerr() this function is used to change the status of the file from error
syntax: void clearerr(FILE *fp);

Pre-processor Commands:

preprocessor is a program that processes source program before compilation.

as it allows us to define macros, the c preprocessor is is also called a macro processor.


a macro is a set of instructions as in a function that does not have a return statement.
a c preprocessor has several directives stating with # symbol

different types of pre-processing directive are :


#define defined symbolic constant
#include include a file
#undef undefine a constant
#ifdef if defined
#if conditional compilation
#else else block of #if
#elif else if condition
#endif end of #if condition
#ifndef if not defined

You might also like