C-File Operations
C-File Operations
What is FILE * fp in C?
FILE *fp ; fp = fopen ("my file . txt", "a"); The function fopen returns a pointer
(referred as the file pointer) to the structure FILE which is defined in the stdio. h
header file . When you open a file make sure that the operation is successful.
File pointer is a pointer which is used to handle and keep track on the files being
accessed. A new data type called “ FILE ” is used to declare file pointer . ...
fopen() function is used to open a file that returns a FILE pointer . Once file is
opened, file pointer can be used to perform I/O operations on the file .
1. Reading
C programming language supports four pre-defined functions to read contents from a file,
defined in stdio.h header file:
1. fgetc()– This function is used to read a single character from the file.
2. fgets()– This function is used to read strings from files.
3. fscanf()– This function is used to read formatted input from a file.
4. fread()– This function is used to read the block of raw bytes from files. This is used to
read binary files.
● Open a file using the function fopen() and store the reference of the file in a FILE
pointer.
● Read contents of the file using any of these functions fgetc(), fgets(), fscanf(), or
fread().
● File close the file using the function fclose().
Example.
Look at the one in your lab exercise first
Write a C program to read data from a text file and display the data on the screen. If file
does not exist, create a file.
Solution:
#include <stdio.h>
#include <stdlib.h> // For exit()
int main()
{
FILE *fp;
char fname1[50],ch;
printf("Enter filename to open for reading : ");
scanf("%s", fname1);
// Open one file for reading
fp = fopen(fname1, "r");
Comp211 1
if (fp == NULL)
{
fp = fopen(fname1, "w");
printf("\n%s file does not exist hence file created..", fname1);
//exit(0);
}
while((ch=fgetc(fp))!=EOF)
{
printf("%c", ch);
}
fclose(fp);
return 0;
}
Output:
In above output, we enter file name source.txt. If this file is present at same location where
your program is saved then it will display the content in that file.
2. Writing
When a programs output or some of the variables has to be saved to a storage location on file
system, the data has to be written to a file. In this tutorial, we shall learn how to write data to a
file using some of the builtin functions of C programming language.
The following functions are used to write data into the file. They are:
● fprintf()
● fputs()
● fputc()
Comp211 2
● fwrite()
fprintf()
The fprintf() is used to write formatted output to stream.It is used to write a set of characters into
a file.
fputs()
fputc()
fwrite()
The fwrite() function is used to write data (can contain multiple characters and multiple lines) to
a file.
example1
#include <stdio.h>
#include <stdlib.h>
int main() {
char sentence[1000];
// exiting program
if (fptr == NULL) {
printf("Error!");
exit(1);
}
printf("Enter a sentence:\n");
fgets(sentence, sizeof(sentence), stdin);
Comp211 3
fprintf(fptr, "%s", sentence);
fclose(fptr);
return 0;
}
Output
Here, a file named program.txt is created. The file will contain C programming is fun text.
In the program, the sentence entered by the user is stored in the sentence variable.
Then, a file named program.txt is opened in writing mode. If the file does not exist, it will be
created.
Finally, the string entered by the user will be written to this file using the fprintf()function and the
file is closed.
Example2
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fptr;
char name[20];
int age;
float salary;
if (fptr == NULL)
{
printf("File does not exist.\n");
return;
}
printf("Enter the name:\n");
scanf("%s", name);
fprintf(fptr, "Name = %s\n", name);
Comp211 4
printf("Enter the age:\n");
scanf("%d", &age);
fprintf(fptr, "Age = %d\n", age);
fclose(fptr);
}
Comp211 5