0% found this document useful (0 votes)
2 views

Introduction to files. T Koki

The document provides an introduction to files in programming, explaining that a file is a collection of related data stored on a disk and outlining typical operations such as opening, reading, writing, and closing files. It distinguishes between text and binary files, describes the use of the fopen() function for opening files with various modes, and emphasizes the importance of closing files properly. Additionally, it includes code examples demonstrating how to read from and write to files in C.

Uploaded by

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

Introduction to files. T Koki

The document provides an introduction to files in programming, explaining that a file is a collection of related data stored on a disk and outlining typical operations such as opening, reading, writing, and closing files. It distinguishes between text and binary files, describes the use of the fopen() function for opening files with various modes, and emphasizes the importance of closing files properly. Additionally, it includes code examples demonstrating how to read from and write to files in C.

Uploaded by

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

Introduction to files

• A file is a collection of related data stored in a particular area on the disk.


• A named collection of data, stored in secondary storage (typically).
• So far, we have been using the stdio.h standard library, which
provides scanf and printf functions for reading from standard input and
writing to standard output respectively.
• reading and writing from a file requires the definition of a file pointer using
file * pointer_name;
• File is an abstract data structure defined in the stdio.h header file.
• There are four Typical operations on files:
• – Open
• – Read
• – Write
• – Close

Tuesday, June 10, 2025 Zano C


Two kinds of Files

• There are two types of files:


– Text :: contains ASCII codes only
– Binary :: can contain non-ASCII characters
e.g. Image, audio, video, executable, etc.

Tuesday, June 10, 2025


Opening Files

• A file must be opened before you can read


from it or write to it.
• The fopen() function is used to open a file.
• It returns the special value NULL to indicate
that it is unable to open the file.
• The fopen() function normally accepts two
arguments, the name or location of file to be
opened and the mode in which the file should
be opened.
Tuesday, June 10, 2025
File mode parameters
Mode meaning
r Opens a file for reading
w Creates a file for writing, and writes over previous
contents(deletes previous contents so be careful)
a Opens a file for appending- writing at the end of the file
Open for both reading and writing. If the file does not exist,
r+
fopen() returns NULL.
Open for both reading and writing. If the file exists, its
w+ contents are overwritten. If the file does not exist, it will be
created.
Open for both reading and appending. If the file does not
a+
exists, it will be created.

Tuesday, June 10, 2025


File mode parameters

• EXAMPLE
• FILE *fptr;
• char filename[]= "file2.dat";
• fptr = fopen (filename,"w"); //opening file
for writing.
• if (fptr == NULL) {
• printf (“ERROR IN FILE CREATION”);
• /* DO SOMETHING */
• }
Tuesday, June 10, 2025
The exit function

• Sometimes error checking means we want an


“emergency exit " from a program.
• In main() we can use return function to stop.
• In functions we can use exit() to do this we use
exit(-1) which is similar to return -1.
• Exit is defined in the stdlib.h library so to use it
we have to include it on pre-processor
directives.
Tuesday, June 10, 2025
Examples1 -writing to a text file
#include <stdio.h>
#include<conio.h>
#include <stdlib.h>
int main() { int num; FILE *fptr; fptr =
fopen("C:\\program.txt","w"); if(fptr == NULL)
{ printf("Error!"); exit(1); } printf("Enter num:
"); scanf("%d",&num); fprintf(fptr,"%d",num);
fclose(fptr); return 0; }

Tuesday, June 10, 2025


Examples1 - Read from a text file using fscanf()
#include <stdio.h>
#include<conio.h>
#include <stdlib.h>
int main() { int num; FILE *fptr; if ((fptr =
fopen("C:\\program.txt","r")) == NULL)
{ printf("Error! opening file"); // Program exits if
the file pointer returns NULL. exit(1); }
fscanf(fptr,"%d",&num); printf("Value of n=
%d",num); fclose(fptr); return 0; }

Tuesday, June 10, 2025


More examples
#include <stdio.h>
#include<conio.h>
#include <stdlib.h>
int main()
{ FILE *fptr;
char filename[]= “Mydetails.txt";
• char name[50];
• clrscr();
• fptr = fopen (filename,"w"); //opening file
for writing.
• if (fptr == NULL) {
• printf (“ERROR IN FILE CREATION”);
• exit(1);
• }

Tuesday, June 10, 2025


Examples

printf("Enter your name”);


scanf("%s",&name);
fprintf(fptr,"\nName :
%s",name);
fclose(fptr);
return 0;
}

Tuesday, June 10, 2025


Closing a File

• When a program terminates it automatically


releases all the allocated memory and close all the
opened files.
• But it is always a good practice that a programmer
should close all the opened files before program
termination.
• The fclose() function is used to close a file.
• fclose accepts the pointer argument and is
implemented as follows:
• fclose(fptr);
Tuesday, June 10, 2025
More Examples

• Write a C program to read name and marks of n


number of students from user and store them in a file.
• #include <stdio.h> int main() { char name[50]; int
marks, i, num; printf("Enter number of students: ");
scanf("%d", &num); FILE *fptr; fptr = (fopen("C:\\
student.txt", "w")); if(fptr == NULL) { printf("Error!");
exit(1); } for(i = 0; i < num; ++i) { printf("For student%d\
nEnter name: ", i+1); scanf("%s", name); printf("Enter
marks: "); scanf("%d", &marks); fprintf(fptr,"\nName:
%s \nMarks=%d \n", name, marks); } fclose(fptr); return
0; }
Tuesday, June 10, 2025
More Examples

• Write a C program to write all the members of an array of


strcures to a file using fwrite(). Read the array from the file and
display on the screen.
• #include <stdio.h> #include <conio.h> struct student { char
name[50]; int height; }; int main(){ struct student stud1[5],
stud2[5]; FILE *fptr; int i; clrscr(); fptr = fopen("file.txt","wb"); for(i
= 0; i < 5; ++i) { fflush(stdin); printf("Enter name: ");
gets(stud1[i].name); printf("Enter height: "); scanf("%d",
&stud1[i].height); } fwrite(stud1, sizeof(stud1), 1, fptr);
fclose(fptr); fptr = fopen("file.txt", "rb"); fread(stud2,
sizeof(stud2), 1, fptr); for(i = 0; i < 5; ++i) { printf("Name: %s\
nHeight: %d", stud2[i].name, stud2[i].height); } fclose(fptr);
getch();return 0; }
Tuesday, June 10, 2025

You might also like