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

Files in C

Uploaded by

Mrs.Minu Meera M
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Files in C

Uploaded by

Mrs.Minu Meera M
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 14

JUSTICE BASHEER AHMED SAYEED COLLEGE FOR

WOMEN
(Autonomous) Afternoon Session Chennai 18.
S.I.E.T.

Prepared by
M. MINU MEERA, Assistant Professor
A. JUNAITHA BARVEEN, Assistant
Professor

06/22/24 Presenter Name: M.MINU MEERA DEPARTMENT OF COMPUTER


,A.JUNAITHA BARVEEN, Department SCIENCE 1
of Computer Science
• Data can be stored on the disk and read whenever necessary, without
destroying the data
• C uses a structure called FILE(defined in stdio.h) to store the attributes of
a file.
• FILE-Place on the disk where a group of related data is stored
• C supports a number of functions that have the ability to perform basic
file operations
-Naming a file
-Opening a file
-Reading data from a file
-Writing data to a file
-Closing a file
06/22/24 Presenter Name: M.MINU MEERA ,A.JUNAITHA BARVEEN, Department 2
of Computer Science
• C provides several different file operations
• fopen-open a file- specify how its opened (read/write) and type
(binary/text)
• fclose-close an opened file
• fread- read from a file
• fwrite- write to a file
• fseek/fsetpos - move a file pointer to somewhere in a file.
• ftell/fgetpos - tell you where the file pointer is located.

06/22/24 Presenter Name: M.MINU MEERA ,A.JUNAITHA BARVEEN, Department 3


of Computer Science
• Writing mode : if file already exists then contents are deleted, – else
new file with specified name created
• Appending mode : if file already exists then file opened with contents
safe – else new file created
• Reading mode : if file already exists then opened with contents safe –
else error occurs.
• Additional modes :
– r+ open to beginning for both reading/writing
– w+ same as w except both for reading and writing
– a+ same as ‘a’ except both for reading and writing

06/22/24 Presenter Name: M.MINU MEERA ,A.JUNAITHA BARVEEN, Department 4


of Computer Science
• Syntax :
FILE *fp; /*variable fp is pointer to type FILE*/
fp= fopen(“filename”, “mode”); /*opens file with name filename , assigns identifier to
fp*/
• Fp
– contains all information about file
– Communication link between system and program
• The file mode tells C how the program will use the file
• The filename indicates the system name and location for the file.
• We assign the return value of fopen to our pointer variable:
fp= fopen(“MYFILE.TXT”, “w”);
fp= fopen(“A:\\MYFILE.TXT”, “w”);

06/22/24 Presenter Name: M.MINU MEERA ,A.JUNAITHA BARVEEN, Department 5


of Computer Science
• When we finish with a mode, we need to close the file before ending the
program or beginning another mode with that same file.
• • To close a file, we use fclose and the pointer variable:
fclose(spData);
• Example:
FILE *p1;
p1 = fopen(“INPUT.txt”, “r”);
……..
……..
fclose(p1);
• pointer can be reused after closing
06/22/24 Presenter Name: M.MINU MEERA ,A.JUNAITHA BARVEEN, Department 6
of Computer Science
• C provides several different functions for reading/writing
• getc() –read a character
• putc() – write a character
• fprintf() – write set of data values
• fscanf() –read set of data values
• getw() –read integer
• putw() – write integer

06/22/24 Presenter Name: M.MINU MEERA ,A.JUNAITHA BARVEEN, Department 7


of Computer Science
• Handle one character at a time like getchar() and putchar()
• syntax: putc(c,fp1);
– c : a character variable
– fp1 : pointer to file opened with mode w
• syntax: c = getc(fp2);
– c : a character variable
– fp2 : pointer to file opened with mode r
• file pointer moves by one character position after every getc()
and putc()
• getc() returns end-of-file marker EOF when file end reached
06/22/24 Presenter Name: M.MINU MEERA ,A.JUNAITHA BARVEEN, Department 8
of Computer Science
06/22/24 Presenter Name: M.MINU MEERA ,A.JUNAITHA BARVEEN, Department 9
of Computer Science
06/22/24 Presenter Name: M.MINU MEERA ,A.JUNAITHA BARVEEN, Department 10
of Computer Science
06/22/24 Presenter Name: M.MINU MEERA ,A.JUNAITHA BARVEEN, Department 11
of Computer Science
• fread()
• Declaration:
fread(void *ptr, size, n, FILE *stream);
• fread reads a specified number of equal-sized data items from an input stream into a block.
– ptr = Points to a block into which data is read
– size = Length of each item read, in bytes
– n = Number of items read
– stream=file pointer
• Example :
char a[10]={'1','2','3','4','5','6','7','8','9','a'};
FILE *fs; fs=fopen("Project.txt","w");
fwrite(a,1,10,fs);
fclose(fs)

06/22/24 Presenter Name: M.MINU MEERA ,A.JUNAITHA BARVEEN, Department 12


of Computer Science
06/22/24 Presenter Name: M.MINU MEERA ,A.JUNAITHA BARVEEN, Department 13
of Computer Science
06/22/24 Presenter Name: M.MINU MEERA ,A.JUNAITHA BARVEEN, Department 14
of Computer Science

You might also like