0% found this document useful (0 votes)
57 views25 pages

File Management in C

The document discusses file management in C and describes how to perform basic file operations like opening, reading, writing, and closing files. It explains that files allow storing of data on disks for flexible access without data loss. The key functions covered are fopen(), fread(), fwrite(), fclose() for file I/O and fseek() for random access of files.
Copyright
© Attribution Non-Commercial (BY-NC)
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)
57 views25 pages

File Management in C

The document discusses file management in C and describes how to perform basic file operations like opening, reading, writing, and closing files. It explains that files allow storing of data on disks for flexible access without data loss. The key functions covered are fopen(), fread(), fwrite(), fclose() for file I/O and fseek() for random access of files.
Copyright
© Attribution Non-Commercial (BY-NC)
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/ 25

File Management in C

(Topic 7: Files GTU Syllabus 2009)


Chaudhari Technical Institute MCA (506) Gandhinagar

Created By: Vinod Pillai. [email protected] http://vinodthebest.wordpress.com

File Management
We have been using the functions such as printf and scanf to read and write data. This works fine as long as data is small. But it has two major problems:
It becomes cumbersome and time consuming to handle large volume of data through terminals. The entire data is lost when either the program is terminated or the computer is turned off.
Prepared By: Vinod Pillai Chaudhari Technical Institute - Gandhinagar

File Management
So it is necessary to have more flexible approach where data can be stored on the disks and read whenever necessary, without destroying data. This concept is called files. Files: A file is a place on the disk where a group of related data is stored.
FILE is a structure declared in stdio.h . We have to use file pointer, a pointer variable that points to a structure FILE.
Prepared By: Vinod Pillai Chaudhari Technical Institute - Gandhinagar
3

C File Management
C supports a number o functions that have the ability to perform basic file operations:
Creating a file Opening a file Reading data from a file Writing data to a file Closing a file Renaming a file Deleting a file
Prepared By: Vinod Pillai Chaudhari Technical Institute - Gandhinagar
4

Using Files in C
Declaration of File Pointer
FILE *fp; FILE *fp1; Opening a File

Checking the result of fopen() if(fp == NULL) { printf(Cant open); exit(1); }

fp=fopen(one.txt,r);

Prepared By: Vinod Pillai Chaudhari Technical Institute - Gandhinagar

Using Files in C
Closing Files
fclose(fp);

Prepared By: Vinod Pillai Chaudhari Technical Institute - Gandhinagar

File Opening Modes


r= w= a= Open file for Reading Create file for Writing Append text file r+ = w+ = a+ = rb = wb= ab = Open binary file for Reading Create binary file for Writing Append binary file Open file for read/write Create file for read/write Append text file for read/write

r+b = Open binary file for read/write w+b= Create binary file for read/write a+b= Append binary file for read/write
7

Prepared By: Vinod Pillai Chaudhari Technical Institute - Gandhinagar

Working with Text files


Four functions for read files from the disk:
fscanf()

Four functions for write files into the disk:


fprintf()

fgets()
fgetc() fread()

fputs()
fputc() fwrite()
8

Prepared By: Vinod Pillai Chaudhari Technical Institute - Gandhinagar

Write Character to a file


Char name[30]; int i=0; FILE *fp; fp=fopen(one.txt,w);
Printf(Enter Name:); Scanf(%*^\n+,name);

While(name*i+!=\0) { putc(name[i],fp); i++; }


fclose(fp);

Prepared By: Vinod Pillai Chaudhari Technical Institute - Gandhinagar

Reading Character from file


int ch; //IMP While(ch!=EOF) { putchar(ch); //IMP ch=getc(fp); }
fclose(fp);

FILE *fp; fp=fopen(one.txt,r); ch=getc(fp);

Prepared By: Vinod Pillai Chaudhari Technical Institute - Gandhinagar

10

Programs
Example 4 : Count the number of characters and number of lines.
Example 6: Compare two files.

int ch1,ch2; FILE *fp1,*fp2; fp1=fopen(one.txt,r); fp2=fopen(two.txt,r);


ch1=getc(fp1); ch2=getc(fp2);

Prepared By: Vinod Pillai Chaudhari Technical Institute - Gandhinagar

11

Programs
while(ch1!=EOF && ch2!=EOF && ch1==ch2) { ch1=getc(fp1); ch2=getc(fp2); }
if(ch1 == ch2) { printf(Same); } else if(ch1 != ch2) { printf(Not Same); } fclose(fp1); fclose(fp2);
12

Prepared By: Vinod Pillai Chaudhari Technical Institute - Gandhinagar

Programs
Example 7: Copy one file to another. fprintf() : Writing to a file
fscanf() : Reading content from file

FILE *fp1;
fp1=fopen(one.txt,w);

fprintf(fp1,%s, Hello); fprintf(fp1,%d,10); fclose(fp1);

Prepared By: Vinod Pillai Chaudhari Technical Institute - Gandhinagar

13

Programs
FILE *fp1; char name[30]; int value;
fp1=fopen(one.txt,r); fscanf(fp1,%s, name); fscanf(fp1,%d,value); fclose(fp1);

Example 13: feof(). char value[30];


FILE *fp; fp=fopen(one.txt,r); while(!feof(fp)) { fgets(value,30,fp); } fclose(fp);
14

Prepared By: Vinod Pillai Chaudhari Technical Institute - Gandhinagar

Binary Files
FILE *fp1,*fp2; fp1=fopen(one.txt,rb); fp2=fopen(two.txt,wb);
int ch;
if(!feof(fp1)) { fputc(ch,fp2); } else { break; }
}

While(1) { ch=fgetc(fp1);

fclose(fp1); fclose(fp2);
15

Prepared By: Vinod Pillai Chaudhari Technical Institute - Gandhinagar

Direct File Input & Output


fread() fwrite() feof() End of file ferror() - Error has occurred. fread() & fwrite() = To read and write block of data. ===================== void main() { FILE *fp1; int i, arr1[10], arr2[10];

Prepared By: Vinod Pillai Chaudhari Technical Institute - Gandhinagar

16

Direct File Input & Output


for(i=0;i<10;i++) { arr1[i]=i * 2; }
fp1=fopen(one.txt,wb);
fclose(fp1);
fread(arr2,sizeof(int),10,fp) fclose(fp1);

for(i=0;i<10;i++) { printf(%d,arr2*i]); fwrite(arr,sizeof(int),10,fp1) }


Prepared By: Vinod Pillai Chaudhari Technical Institute - Gandhinagar
17

IMP Programs
Example 16.
Example 17.

(fprintf & fscanf)


(fread & fwrite)

Prepared By: Vinod Pillai Chaudhari Technical Institute - Gandhinagar

18

Storing Structure in File


Struct item { int code; double price; };
Void main() {

struct item t1,t2; File *fp; fp=fopen(one.txt,wb);


t1.code=10; t1.price=10.56; fwrite(&t1,sizeof(t1),1,fp); fclose(fp);
19

Prepared By: Vinod Pillai Chaudhari Technical Institute - Gandhinagar

Storing Structure in File


fp=fopen(one.txt,rb);
Fread(&t2,sizeof(t2),1,fp);

fclose(fp);

Prepared By: Vinod Pillai Chaudhari Technical Institute - Gandhinagar

20

Random Access of Files


fseek() = To Jump to particular Location in a File
ftell() = Current Location in a File

rewind() = Back to First Location in a File

Prepared By: Vinod Pillai Chaudhari Technical Institute - Gandhinagar

21

Random Access of Files (fseek())


int fseek(FILE *FP, long offset, int orgin);
fp

0 = Successfully
Starting Point:

Non-Zero = Error
SEEK_SET ( 0 Beginning File)

SEEK_CUR (1- Current File)


Distance position indicator to move in bytes

SEEK_END (2 End of file)

Prepared By: Vinod Pillai Chaudhari Technical Institute - Gandhinagar

22

Rewind() & ftell()


Void rewind(fp); Long ftell(fp); value=ftell(fp);

Prepared By: Vinod Pillai Chaudhari Technical Institute - Gandhinagar

23

Renaming & Deleting File


Deleting a File:
int remove(filename);

Renaming a File:
int rename(old_name,new _name);

0 = Success 1 = Failure

0 = Success 1 = Failure

Prepared By: Vinod Pillai Chaudhari Technical Institute - Gandhinagar

24

References
Programming in ANSI C Edition 2.1 by E Balagurusamy.
Programming in C by Paradip Dey & Manas Ghosh.

Prepared By: Vinod Pillai Chaudhari Technical Institute - Gandhinagar

25

You might also like