0% found this document useful (0 votes)
32 views5 pages

Lecture Eight

Uploaded by

alvinmwangisir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views5 pages

Lecture Eight

Uploaded by

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

UNIVERSITY OF EMBU

SCHOOL OF PURE AND APPLIED SCIENCES


DEPARTMENT OF MATHEMATICS, COMPUTING & IT

STA 121: PROGRAMMING METHODOLOGY

WRITTEN BY: EDITED BY:


MR JOHN KIRWA

Copyright © UNIVERSITY OF EMBU, AUGUST 2020


All Rights Reserved
LESSON 8: FILE HANDLING

Data stored in variables and arrays is temporary — it’s lost when the program terminates. C
allows a program to read data from a file or write data to a file. Once the data is saved in a file on
a computer disk, it will remain there after the program stops running. The data can then be
retrieved and used at a later time.

There are two types of files in C:

 Text files
 Binary files.

A text file is processed as a sequence of characters. In a text file, there is a special end-of-line
symbol that divides the file into lines. In addition, you can think that there is a special end-of-file
symbol that follows the last component in a file. A big advantage of text files is that it may be
opened and viewed in a text editor such as Notepad.

A binary file stores data that has not been translated into character form. Binary files typically
use the same bit patterns to represent data as those used to represent the data in the computer's
main memory. These files are called binary files because the only thing they have in common is
that they store the data as sequences of zeros and ones.

Steps to process file Input/Output in C:

1. Declare file pointer.


2. Open the file.
3. Read/write data to the file.
4. Close the file, when the program is finished.

Writing Data to a Text File

#include <stdio.h>

int main()
{
FILE *out_file; /* file pointer */
int number;
/* Open file in write mode */
out_file = fopen("sample.txt", "w");

if (out_file == NULL)
{
printf("Can't open file for writing.\n");
}
else
{
for (number = 1; number <= 10; number++)
{
/* Write data to file */
fprintf(out_file, "%d ", number);
}
/* Close the file */
fclose(out_file);
}
return 0;
}

Function ‘fopen’ takes two arguments: a filename (which can include path information leading to
the file’s location) and a file open mode. The file open mode "w" indicates that the file is to be
opened for writing. If a file does not exist and it’s opened for writing, fopen creates the file. If an
existing file is opened for writing, the contents of the file are discarded without warning. In the
program, the if statement is used to determine whether the file pointer out_file is NULL (i.e., the
file is not opened). If it’s NULL, the program prints an error message and terminates. Otherwise,
the program processes the input and writes it to the file.

File open mode

Mode Description

r Open an existing file for reading.

w Create a file for writing. If the file already exists, discard the current contents.

a Append: open or create a file for writing at the end of the file.

r+ Open an existing file for update (reading and writing).


w+ Create a file for update. If the file already exists, discard the current contents.

a+ Append: open or create a file for an update; writing is done at the end of the file.

rb Open an existing file for reading in binary mode.

wb Create a file for writing in binary mode. If the file already exists, discard the
current contents.

ab Append: open or create a file for writing at the end of the file in binary mode.

rb+ Open an existing file for update (reading and writing) in binary mode.

wb+ Create a file for an update in binary mode. If the file already exists, discard the
current contents.

ab+ Append: open or create a file for an update in binary mode; writing is done at the
end of the file.

Reading Data from Text File

#include <stdio.h>

int main()
{
char in_name[80];
FILE *in_file;
int ch;

printf("Enter file name:\n");


scanf("%s", in_name);

in_file = fopen(in_name, "r");

if (in_file == NULL)
{
printf("Can't open %s for reading.\n", in_name);
}
else
{
while ((ch = fgetc(in_file)) != EOF)
{
printf("%c", ch);
}
fclose(in_file);
}
return 0;
}

Related links

The development of C language by Dennis M. Ritchie

DJGPP a complete 32-bit C/C++ development system for Intel 80386 and higher PCs.

List of Free C Compilers and interpreters available on the internet.

https://www.sanfoundry.com/c-programming-examples/

You might also like