Programming in 
C
What is File? 
File is a connection of bytes stored in our secondary device. We 
frequently use these files for storing different information 
which can be process by our program. So in order to store the 
information permanently we need to use these files. 
Bytes 
--------- 
--------- 
file 
Header file 
There are two type of Files : 
• Text Files(contains ASCII) 
• Binary Files(contains Non- ASCII)(E.g.- audio, video, pictures)
Steps in Processing a File 
1. Create the stream via a pointer variable using the 
FILE structure: 
FILE *p; 
2. Open the file, associating the stream name with the 
file name. 
3. Read or write the data. 
4. Close the file.
Library Functions For Files I/O 
Different library functions are used for files input 
& output. These routines are used to open , 
close, read ,write file using formatted 
Input/output. 
1. fopen() 
2. fclose() 
3. fflush()
• fopen() : This function is used to open a file for 
formatted I/O & to associate a stream with that 
file. A stream is a destination of data. It may be 
buffer in the memory of a file or some hardware 
device such as a port. 
Syntax: FILE *fptr 
fptr=fopen(“file name", "mode”);
Modes used for opening a file: 
• r - open for reading an existing file. 
• w - Creating a new file for writing only. If file with same 
name exits, it will be overwritten. 
• a - Open an existing file & append the contents at the end 
of that file. If file does not exist, it will create a new file. 
• r+ - Open files for update(reading & writing). 
• W+ - Open Or create file for update. Discard any 
previous data. 
• a+ - Open or create file for update Write after any 
previous data.
• fclose() : This function is used to close the file. 
Syntax: int fclose(fptr); 
• fflush() :This function used for clearing the buffer 
before closing the file. It flushes the currently 
available pending data of files. All the output units 
will be flushed, in case the parameter is NULL. It is 
useful when some set of writes has completed 
before, like responding to a request. 
Syntax: int fflush(file* stream)
Character Input/output 
1. fputc() 
2. fgetc() 
• fputc() : fputc writes a single character or constant 
to the output stream. It returns at end of the file on 
failure. 
Syntax: int fputc(c,fptr); 
• fgetc() : fgetc is used to abstract a single character 
from any given file. 
Syntax: int fgetc(fptr);
String Input/output 
1. fputs() 
2. fgets() 
• fputs() : This function is used to writing 
a string to a file. 
Syntax : int fputs(sptr,fptr); 
• fgets() : This function is used to take 
strings to the file. 
Syntax : a=fgets(str,int n,fptr);
Formatted Input/output 
1. fprintf() 
2. fscanf() 
• fprintf()- Data is written to a file using this 
function. fprintf() is very similar to printf(). 
Syntax: int fprintf(fptr,””format-string”,items); 
• fscanf - Data is read from a file using fsanf(). 
fscanf() is very similar to scanf(). 
Syntax: int fscanf(fptr, “format-string”,items);
Random Access to file 
• fseek() : This function is used to place our 
cursor to a certain position in our file. 
Syntax : fseek(fptr,offset,mode); 
Modes: 
0(SEEK_SET)represents the beginning of the 
file. 
1(SEEK_CUR)represents the current position of 
the file. 
2(SEEK_END)represents the end of the file.

Programming in C Session 4

  • 1.
  • 2.
    What is File? File is a connection of bytes stored in our secondary device. We frequently use these files for storing different information which can be process by our program. So in order to store the information permanently we need to use these files. Bytes --------- --------- file Header file There are two type of Files : • Text Files(contains ASCII) • Binary Files(contains Non- ASCII)(E.g.- audio, video, pictures)
  • 3.
    Steps in Processinga File 1. Create the stream via a pointer variable using the FILE structure: FILE *p; 2. Open the file, associating the stream name with the file name. 3. Read or write the data. 4. Close the file.
  • 4.
    Library Functions ForFiles I/O Different library functions are used for files input & output. These routines are used to open , close, read ,write file using formatted Input/output. 1. fopen() 2. fclose() 3. fflush()
  • 5.
    • fopen() :This function is used to open a file for formatted I/O & to associate a stream with that file. A stream is a destination of data. It may be buffer in the memory of a file or some hardware device such as a port. Syntax: FILE *fptr fptr=fopen(“file name", "mode”);
  • 6.
    Modes used foropening a file: • r - open for reading an existing file. • w - Creating a new file for writing only. If file with same name exits, it will be overwritten. • a - Open an existing file & append the contents at the end of that file. If file does not exist, it will create a new file. • r+ - Open files for update(reading & writing). • W+ - Open Or create file for update. Discard any previous data. • a+ - Open or create file for update Write after any previous data.
  • 7.
    • fclose() :This function is used to close the file. Syntax: int fclose(fptr); • fflush() :This function used for clearing the buffer before closing the file. It flushes the currently available pending data of files. All the output units will be flushed, in case the parameter is NULL. It is useful when some set of writes has completed before, like responding to a request. Syntax: int fflush(file* stream)
  • 8.
    Character Input/output 1.fputc() 2. fgetc() • fputc() : fputc writes a single character or constant to the output stream. It returns at end of the file on failure. Syntax: int fputc(c,fptr); • fgetc() : fgetc is used to abstract a single character from any given file. Syntax: int fgetc(fptr);
  • 9.
    String Input/output 1.fputs() 2. fgets() • fputs() : This function is used to writing a string to a file. Syntax : int fputs(sptr,fptr); • fgets() : This function is used to take strings to the file. Syntax : a=fgets(str,int n,fptr);
  • 10.
    Formatted Input/output 1.fprintf() 2. fscanf() • fprintf()- Data is written to a file using this function. fprintf() is very similar to printf(). Syntax: int fprintf(fptr,””format-string”,items); • fscanf - Data is read from a file using fsanf(). fscanf() is very similar to scanf(). Syntax: int fscanf(fptr, “format-string”,items);
  • 11.
    Random Access tofile • fseek() : This function is used to place our cursor to a certain position in our file. Syntax : fseek(fptr,offset,mode); Modes: 0(SEEK_SET)represents the beginning of the file. 1(SEEK_CUR)represents the current position of the file. 2(SEEK_END)represents the end of the file.

Editor's Notes

  • #2 By prerna sharma