You are on page 1of 24

Files

………………..
Contents
• File definition
• Opening and closing a data file
• Reading and writing a data file
• Files I/O Functions
Why files are needed?
• In C programming, file is a place on your physical disk where
information is stored.
• When a program is terminated, the entire data is lost. Storing in a file
will preserve your data even if the program terminates.
• 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 few commands in C.
• You can easily move your data from one computer to another without
any changes.

Types of Files
When dealing with files, there are two types of files you should know about:
Text files
Binary files
1. Text files//c files
Text files are the normal .txt files that you can easily create using Notepad or any simple text editors.
When you open those files, you'll see all the contents within the file as plain text. You can easily edit or delete
the contents.
They take minimum effort to maintain, are easily readable, and provide least security and takes bigger storage
space.
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 higher amount of data, are not readable easily and provides a better security than text files.
File Operations

• In C, you can perform four major operations on the file, either text or
binary:
1. Creating a new file
2. Opening an existing file
3. Closing a file
4. Reading from and writing information to a file
Working with files

• When working with files, you need to declare a pointer of type file.
This declaration is needed for communication between the file and
program.

• FILE *a;
Opening a file - for creation and edit
• Opening a file is performed using the library function in the "stdio.h"
header file: fopen().

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

• ptr = fopen("fileopen","mode")

• For Example:

• fopen("E:\\cprogram\\newprogram.txt","w");
Basic file operations.
Function description

fopen() create a new file or open a existing file

fclose() closes a file

getc() reads a character from a file

putc() writes a character to a file

fscanf() reads a set of data from a file

fprintf() writes a set of data to a file

getw() reads a integer from a file

putw() writes a integer to a file


Opening Modes in Standard I/O
File Mode Meaning of Mode During Inexistence of file

If the file does not exist, fopen() returns


r Open for reading.
NULL.
If the file exists, its contents are
w Open for writing./creating a file overwritten. If the file does not exist, it
will be created.

Open for append. i.e, Data is added to end of If the file does not exists, it will be
a
file. created.
Continued…

If the file does not exist, fopen() returns


r+ Open for both reading and writing.
NULL.

If the file exists, its contents are overwritten.


w+ Open for both reading and writing.
If the file does not exist, it will be created.

a+ Open for both reading and appending. If the file does not exists, it will be created.
How to Create a File
• To create a file in a 'C' program following syntax is used,
FILE *fp;
fp = fopen ("file_name", "mode");
• Example:
#include <stdio.h>
int main()
{
FILE *fp;
fp = fopen ("data.txt", "w");
}
How to Close a file
'C' provides the fclose function to perform file closing operation. The
syntax of fclose is as follows,
• fclose (file_pointer);
Example:
FILE *fp;
fp = fopen ("data.txt", "r");
fclose (fp);
Writing to a File
• In C, when you write to a file, newline characters '\n' must be explicitly
added.
• The stdio library(stdlib.h) offers the necessary functions to write to a file:
• fputc(char, file_pointer): It writes a character to the file pointed to by
file_pointer.
• fputs(str, file_pointer): It writes a string to the file pointed to by
file_pointer.
• fprintf(file_pointer, str, variable_lists): It prints a string to the file pointed
to by file_pointer. The string can optionally include format specifiers and a
list of variables variable_lists.
fputc() Function:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i;
FILE * p;
char s[100] = "Programming on files ";
p = fopen("ft.txt", "w");
for (i = 0; s[i] != '\0'; i++)
{
fputc(s[i],p);
}
fclose(p);
return 0;
}
fputs () Function:
#include <stdio.h>
int main()
{
FILE * fp;
fp = fopen("ft.txt", "w");
fputs("I love programming\n,", fp);
fputs("very interesting subject\n", fp);
fputs("easy to learn\n", fp);
fputs("easy to understand", fp);

fclose(fp);
return (0);
}
fprintf()Function:
#include <stdio.h>
int main()
{
FILE *p;
p = fopen("ft.txt", "w");
fprintf(p,"I love programming\n");
fprintf(p,"very interesting subject\n");
fclose(p);
return 0;
}
Reading data from a File
fgetc(file_pointer): It returns the next character from the file pointed
to by the file pointer. When the end of the file has been reached, the
EOF is sent back.
fopen,fclose,fgetc and r mode:
#include <stdio.h>
int main()
{
char a;
FILE *p;
p = fopen(”sample.c","r");

while((a=fgetc(p))!=EOF)
{
printf("%c", a);
}
fclose(p);
return 0;
}
fopen,fclose,fprintf and append mode:
#include <stdio.h>
int main()
{
FILE * fp;
fp = fopen("ft.txt", "a");
fputs("I love programming\n,", fp);
fprintf(fp,"very interesting subject\n");
fclose(fp);
return (0);
}
File copying:
#include <stdio.h>
#include <stdlib.h>
int main()
{
char ch, source_file[20], target_file[20];
FILE *source, *target;
printf("Enter name of file to copy\n");
gets(source_file);
source = fopen(source_file, "r");
printf("Enter name of target file\n");
gets(target_file);
target = fopen(target_file, "w");
while ((ch = fgetc(source)) != EOF)
fputc(ch, target);
printf("File copied successfully.\n");
fclose(source);
fclose(target);
return 0;
}
Output:
Enter name of file to copy
fwrite.c
Enter name of target file
sample.txt
File copied successfully.
Counting number of lines:
#include <stdio.h>
#include <stdlib.h>
int main()
{
char a;
FILE *fp;
int lines=0;
fp = fopen("fwrite.c","r");
while((a=fgetc(fp))!=EOF)
if(a=='\n')
{
lines=lines+1;
}
fclose(fp);
printf("Total number of lines are: %d\n",lines);
return 0;
}
Counting number of words:
#include <stdio.h>
#include <stdlib.h>
int main()
{
char a;
FILE *fp;
int words=0;
fp = fopen("fwrite.c","r");
while((a=fgetc(fp))!=EOF)
if (a == ' ' || a == '\n')
{
words=words+1;
}

fclose(fp);
printf("Total number of words are: %d\n",words);
return 0;
}
Counting number of characters:
#include <stdio.h>
#include <stdlib.h>
int main()
{
char a;
FILE *fp;
int characters=0;
fp = fopen("fwrite.c","r");
while((a=fgetc(fp))!=EOF)
if (a != ' ' || a != '\n')
{
characters=characters+1;
}

fclose(fp);
printf("Total number of characters are: %d\n",characters);
return 0;
}

You might also like