Files in Computer Programming
Files in Computer Programming
Agenda
• File concept
• File pointer
• File handling operations (open, close, read, write etc) on
sequential and random access files.
• Programs on file manipulations using fgetc(), fgets(),fseek()
File concept
Why do we need File Handling in C?
• The output of a C program is generally deleted when the program is closed.
Sometimes, we need to store that output for different purposes.
Some uses of file handling
• Reusability: The file-handling process keeps track of the information created after
the program has been run.
• Portability: Without losing any data files can be transferred to another in the
computer system. The risk of flawed coding is minimized with this feature.
• Efficient: A large amount of input may be required for some programs. File
handling allows you to easily access a part of a code using individual commands
which saves a lot of time and reduces the chance of errors.
• Storage Capacity: Files allow you to store data without having to worry about
storing everything simultaneously in a program.
Types of Files
FILE *fptr;
Opening a file - for creation and edit
fclose(fptr);
Reading and writing to a text file
• For reading and writing to a text file, we use the functions fprintf() and fscanf().
In the given syntax, the filename and the mode are specified as strings
hence they must always be enclosed within double quotes.
How to Close a file
Write to a text file
Read from a text file
Input/Output Operations on File
getc and putc Functions
Simplest I/O functions are getc and putc.
• Handles one character at a time.
• Syntax:
putc(char, FILE *fp);
getc(FILE *fp);
• Eg:
putc(c, fp1);
c=getc(fp2);
Example Program 2
/* Program to read data from the keyboard, write it to a file called
INPUT, again read the same data from the INPUT file and display
it on the screen */
Enumerated data type (module 3)
• Incomputer programming enumerated data type (enum) is used
to create the group of constants.
int main()
{
enum weekdays w; // variable declaration of weekdays type
w=Monday; // assigning value of Monday to w.
printf("The value of w is %d",w);
return 0;
}
Output
The value of w is 2
#include <stdio.h>
enum months{jan=1, feb, march, april, may, june, july, august, september, o
ctober, november, december};
int main()
{
// printing the values of months
for(int i=jan;i<=december;i++)
{
printf("%d, ",i);
}
return 0;
}
Output
1,2,3,4,5,6,7,8,9,10,11,12
Call by value and call by reference
(mod 3)
• Thereare two methods to pass the data into the function in C
language, i.e.,
• 1) call by value
• 2) call by reference.
• In call by value method, the value of the actual parameters is copied into the
formal parameters.
• we can not modify the value of the actual parameter by the formal parameter.
• In call by value, different memory is allocated for actual and formal parameters
since the value of the actual parameter is copied into the formal parameter.
Call by reference
• In call by reference, the address of the variable is passed into the function call as
the actual parameter.
• The value of the actual parameters can be modified by changing the formal
parameters since the address of the actual parameters is passed.