Topic 11 - File Operations
Topic 11 - File Operations
Topic 11 - File Operations
File Operations
ECE1016 – Computer and Program Design
ERC1036 – Computer and Programming
© Multimedia University
Objectives
© Multimedia University 2
Outline
• Introduction
• Basic File Operations
• Examples of File Handling
© Multimedia University 3
Introduction
Topic 11: File Operations
© Multimedia University 4
Introduction
2. 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.
3. You can easily move your data from one computer to another without
any changes.
© Multimedia University 5
Types of Files
2. Binary files
• Binary files are the .bin files.
• Instead of storing data in plain text, they store it in the binary form
(0's and 1's).
© Multimedia University 6
Basic File Operations
Topic 11: File Operations
© Multimedia University 7
Basic File Operations
© Multimedia University 8
1. Opening a file
• Example:
FILE *fp;
fp = fopen("C:\\myfiles\\newfile.txt", "r");
© Multimedia University 9
File Opening Checking
if (fpr == NULL)
{
puts("Error while opening file");
return 1;
}
© Multimedia University 10
File Opening Modes
Mode Description Remarks
r Open for reading fopen() returns NULL if the file does not exist.
w Open for writing If the file exists, its contents are overwritten.
If the file does not exist, it will be created.
a Open for append. Data is If the file does not exist, it will be created.
added to the end of the file
r+ Open for both reading and fopen() returns NULL if the file does not exist.
writing
w+ Open for both reading and If the file exists, its contents are overwritten.
writing If the file does not exist, it will be created.
a+ Open for both reading and If the file does not exist, it will be created.
appending.
© Multimedia University 11
2. Closing a file
• Example:
FILE *fp;
fp = fopen("C:\\myfiles\\newfile.txt", "r");
fclose(fp);
© Multimedia University 12
Example 1: Opening and Closing a File
#include <stdio.h>
int main(){
FILE *fPtr; /* File pointer declaration */
return 0;
}
© Multimedia University 13
3. Reading a file
© Multimedia University 14
4. Writing a file
© Multimedia University 15
Read and Write a File
Equivalent
Function standard Usage
functions
fgetc (FILE *fPtr) getc Reads and returns the next character from
the stream fPtr.
fgets (char *Str, int n, gets Reads characters from stream fPtr into the
FILE *fPtr) string pointed to by Str.
The integer argument n indicates the
maximum number of characters that the
buffer Str can store.
fscanf (FILE *fPtr, const scanf Converts read characters according to the
char *format, ...) format string and the values created are
stored through the argument pointers.
fputc (int c, FILE* fPtr) putc Writes the character c to the file stream
fPtr.
fputs (const char *Str, puts Writes the string Str (excluding the
FILE *fPtr) terminating '\0') to the stream fPtr.
fprintf (FILE *fPtr, const printf Writes formatted data to the file stream fPtr.
char *format, arg0...
argn)
© Multimedia University 16
Examples of File Handling
Topic 11: File Operations
© Multimedia University 17
Example 2: Write to a text file
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num;
FILE *fptr;
fptr = fopen("input.txt","w");
if(fptr == NULL)
{
printf("Error in opening file!\n");
return 1;
}
fprintf(fptr,"%d",num);
fclose(fptr);
return 0;
}
© Multimedia University 18
Example 3: Read from a text file
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *fptr;
int num;
return 0;
} © Multimedia University 19
Example 4: Prompt user for filename and display file
content on screen
#include <stdio.h>
int main()
{
FILE *fPtr;
int c ;
char filename[40] ;
printf("Enter file to be displayed: ");
gets( filename ) ;
fPtr = fopen( filename, "r");
if (fPtr == NULL) {
printf("Error in opening file\n");
return 1;
}
c = fgetc(fPtr) ;
while (c != EOF )
{ putc(c,stdout);
c = fgetc (fPtr); }
fclose(fPtr);
return 0;
}
© Multimedia University 20
Example 5a: Get input from user and save to a text file.
#include <stdio.h>
int main( ) {
int i; //Loop variable
int number; //Stores phone number entered
FILE *fPtr; //File pointer declaration
fPtr = fopen("phone.txt","w"); //Creates file for Write only
Sample output:
© Multimedia University 22
Example 5b: Read data from phone.txt file and display
on screen
#include <stdio.h>
int main( ) {
int i; //Loop variable
int number; //Stores phone number read from file
return 0;
}
© Multimedia University 24
Example 5c: Sample Output
Sample output:
Enter the new phone number dialed
9999
© Multimedia University 25
Summary
© Multimedia University 26