file-handling-C
file-handling-C
Real-life applications
• Opening a file
• Reading data from a file
• Writing data to a file
• Closing a file
C File Handling Operations
FILE *empl ;
char filename[25];
scanf (“%s”, filename);
empl = fopen (filename, “r”) ;
Closing a File
• After all operations on a file have been
completed, it must be closed.
– Ensures that all file data stored in memory buffers
are properly written to the file.
• General format: fclose (file_pointer) ;
FILE *xyz ;
xyz = fopen (“test.txt”, “w”) ;
…….
fclose (xyz) ;
Files and Streams
• Read/Write functions in standard library
– getc
• Reads one character from a file
• Takes a FILE pointer as an argument
• fgetc( stdin ) equivalent to getchar()
– putc
• Writes one character to a file
• Takes a FILE pointer and a character to write as an
argument
• fputc( 'a', stdout ) equivalent to putchar(
'a' )
– fscanf / fprintf
• File processing equivalents of scanf and printf
Read/Write Operations on Files
• The simplest file input-output (I/O) function are getc and putc.
• getc is used to read a character from a file and return it.
char ch; FILE *fp;
…..
ch = getc (fp) ;
– getc will return an end-of-file marker EOF, when the end of
the file has been reached.
• putc is used to write a character to a file.
char ch; FILE *fp;
……
putc (ch, fp) ;
Jaypee Institute of Information Technology University,
Noida
fscanf and fprintf
• We can also use the file versions of scanf and printf, called fscanf
and fprintf.
• General format:
fscanf (file_pointer, control_string, list) ;
fprintf (file_pointer, control_string, list) ;
• Examples:
fscanf (fp, “%d %s %f”, &roll, dept_code, &cgpa) ;
fprintf (out, “\nThe result is: %d”, xyz) ;
fprintf
• Used to print to a file
• It is like printf, except first argument is a FILE pointer (pointer to the file
you want to print in)
Some Points
• How to check EOF condition when using
fscanf?
– Use the function feof
if (feof (fp))
printf (“\n Reached end of file”) ;
• How to check successful open?
– For opening in “r” mode, the file must exist.
if (fp == NULL)
printf (“\n Unable to open file”) ;
• #include <stdio.h>
//reading contents from one file and printing on console
int main()
{
FILE *fp;
int n;
fp=fopen("d1.txt","r");
if(fp==NULL)
printf("error");
fscanf(fp,"%d",&n);
printf("%d",n);
}
Example program: writing into a text file
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num;
FILE *fptr; // use appropriate location if you are
using MacOS or Linux
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;
}
//writing contents from one file and writing contents into
another file
int main()
{
char num[10];
FILE *fptr,*fptr1; // use appropriate location if you are
using MacOS or Linux
fptr1=fopen("sup1.txt","r");
fptr = fopen("sup.txt","w");
if(fptr == NULL)
{
printf("Error!");
exit(1);
}
fscanf(fptr1,"%s",num);
fprintf(fptr,"%s",num);
fclose(fptr);
return 0;
}
Program to read/write using getc/putc
#include <stdio.h>
main()
{ FILE *fp1;
char c;
f1= fopen(“INPUT”, “w”); /* open file for writing */
fclose(f1);
} /*end main */
getw() and putw()
• handle one integer at a time
• syntax: putw(i,fp1);
– i : an integer variable
– fp1 : pointer to file ipened with mode w
• syntax: i = getw(fp2);
– i : an integer variable
– fp2 : pointer to file opened with mode r
• file pointer moves by one integer position, data stored in binary format native to
local system
• getw() returns end-of-file marker EOF when file end reached
1. Text files (ASCII) :Text files contain ASCII codes of digits, alphabetic and symbols.
2. Binary files: Binary file contains collection of bytes (0’s and 1’s). Binary files are
compiled version of text files.
C program using getw, putw,fscanf, fprintf
#include <stdio.h> #include <stdio.h>
main() main()
{ int i,sum1=0; { int i, sum2=0;
FILE *f1; FILE *f2;
/* open files */ /* open files */
f1 = fopen("int_data.bin","w"); f2 = fopen("int_data.txt","w");
/* write integers to files in binary /* write integers to files in binary and
and text format*/ text format*/
for(i=10;i<15;i++) putw(i,f1); for(i=10;i<15;i++) printf(f2,"%d\n",i);
fclose(f1); fclose(f2);
f1 = fopen("int_data.bin","r"); f2 = fopen("int_data.txt","r");
while((i=getw(f1))!=EOF) while(fscanf(f2,"%d",&i)!=EOF)
{ sum1+=i; { sum2+=i; printf("text file: i=%d\
printf("binary file: i=%d\n",i); n",i);
} /* end while getw */ } /*end while fscanf*/
printf("binary sum=%d,sum1); printf("text sum=%d\n",sum2);
fclose(f1); fclose(f2);
} }
On execution of previous Programs
$ ./a.out $ ./a.out
binary file: i=10 text file: i=10
binary file: i=11 text file: i=11
binary file: i=12 text file: i=12
text file: i=13
binary file: i=13
text file: i=14
binary file: i=14 text sum=60
binary sum=60, $ more int_data.bin
$ cat int_data.txt ^@^@^@^K^@^@^@^L^@^@^@^
M^@^@^@^N^@^@^@
10
$
11
12
13
14
Errors that occur during I/O
fp = fopen(“input.dat”, “r”);
if (fp == NULL)
printf(“File could not be opened \n ”);