Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
5 views

Files in Computer Programming

Files
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Files in Computer Programming

Files
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

FILES

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

There are two types of files


• Text files
• Binary files
1. Text files
• Text files are the normal .txt files.
• You can easily create text files using any simple text editors such as Notepad.
• All the contents within the file are plain text.
2. Binary files
• Binary files are mostly the .bin files in your computer.
• Instead of storing data in plain text, they store it in the binary form (0's and
1's).
• They can hold a higher amount of data, are not readable easily, and provides
better security than text files.
The following operations can be performed on a file.

• Creation of a new file (fopen())


• Opening an existing file (fopen())
• Reading from file (fscanf() or fgets())
• Writing to a file (fprintf() or fputs())
• Moving to a specific location in a file (fseek(), rewind())
• Closing a file (fclose())
Functions for file handling
Working with files

• you need to declare a pointer of type file.


• This declaration is needed for communication between the file and the program.

FILE *fptr;
Opening a file - for creation and edit

The syntax for opening a file in standard I/O is:


ptr = fopen("fileopen","mode");
Eg:
fopen("E:\\cprogram\\newprogram.txt","w");
fopen("E:\\cprogram\\oldprogram.bin","rb");
Closing a File
• Thefile (both text and binary) should be closed after
reading/writing.
• Closing a file is performed using the fclose() function.

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.

• Itis a user-defined data type that consists of integer values, and


it provides meaningful names to these values.

• Bydefault, const1 is 0, const2 is 1 and so on. You can change


default values of enum elements during declaration (if necessary).
For example:
enum fruits{mango, apple, strawberry, papaya};

• The default value of mango is 0, apple is 1, strawberry is 2, and papaya is 3.


If we want to change these default values, then we can do as given below:

enum fruits{ mango=2, apple=1, strawberry=5, papaya=


7, };
Example
#include <stdio.h>
enum weekdays{Sunday=1, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday};

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.

The parameters passed to the function are called actual


parameters whereas the parameters received by the function are
called formal parameters.
1) call by value

• 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.

You might also like