File Handling Note For C Programming
File Handling Note For C Programming
#include<stdio.h>
int main()
{
FILE *fp;
char ch;
fp = fopen("first.txt","w"); //OPENS in writing mode
printf("Enter characters");
do{
ch = getchar(); //take a character input from user
fputc(ch, fp); //write this character into the file
} while(ch != '\n');
fclose(fp);
fp = fopen("first.txt", "r");
while(1){
ch = fgetc(fp);
if (ch == EOF)
break;
else
putchar(ch);
}
return 0;
}
FILE *fp;
char str[100];
fp = fopen("second.txt","w");
printf("Enter strings: ");
while(1){
gets(str);
if(strlen(str)>0){
fputs(str, fp);
fputs("\n", fp); //because fputs() doesn't automatically append the newline
}
else
break;
}
fclose(fp);
fp = fopen("second.txt", "r");
while(fgets(str,99,fp)!=NULL){
puts(str);
}
fclose(fp);
return 0;
}
NOTE: fgets (targetstring_array, buffer_size, source_file_pointer);
targetstring_array is the string array to which we want to store the string being read
buffer_size prevents from reading in too many string than can be handled
source_file_pointer is the pointer to the file from which we’re reading
#include<stdio.h>
Int main(){
FILE *fp;
char name[100], ch;
int roll;
float marks;
fp = fopen(“third.txt”, “r+”);
if(fp == NULL)
exit(1);
while(fscanf(fp, “%s%d%f”, name, &roll, &marks) != EOF){
printf(“Name: %s\n Roll: %d\n Marks:%f\n”, name, roll, marks);
}
while(1){
printf(“Enter name, roll and marks: “);
scanf(“%s%d%f”, name, &roll, &marks);
fprintf(fp, “%s %d %f” , name, roll, marks);
printf(“if you do not have next data set, enter n”);
scanf(“%c”, ch);
If (ch == ‘n’)
break;
}
rewind(fp); //points the pointer to the start of the file
while(fscanf(fp, “%s%d%f”, name, &roll, &marks)!=EOF){
printf(“Name: %s\n Roll:%d\n Marks:%f\n”,name, roll, marks);
}
fclose(fp);
return 0;
}
FILE *fp;
int i;
struct student{
char name[30];
int marks;
};
struct student s;
if((fp = fopen("student.txt", "w+")) == NULL)
exit(1);
for (i= 0 ; i<20 ; i++){
printf("Enter name and marks:");
fscanf(stdin,"%s%d",s.name, &s.marks);
fprintf(fp, "%s %d", s.name, s.marks);
}
rewind(fp);
while(fscanf(fp, "%s%d", s.name, &s.marks)!= EOF){
fprintf(stdout, "Name: %s\n Marks: %d\n", s.name, s.marks);
}
fclose(fp);
return 0;
}
All the programs that we wrote so far worked on text files. Some of them would not work correctly on binary
files. A text file contains only textual information like alphabets, digits and special symbols. In actuality the
ASCII codes of these characters are stored in text files. A good example of a text file is any C program, say
PR1.C .
As against this, a binary file is merely a collection of bytes. This collection might be a compiled version of a C
program (say PR1.EXE), or music data stored in a wave file or a picture stored in a graphic file. A very easy way
to find out whether a file is a text file or a binary file is to open that file in Turbo C/C++. If on opening the file
you can make out what is displayed then it is a text file, otherwise it is a binary file.
[Excerpt from Let US C]
Example of a File copy program that works for binary as well as text files.
#include "stdio.h"
void main( )
{
FILE *fs, *ft ;
int ch ;
fs = fopen ( "pr1.exe", "rb" ) ; //rb,wb,etc means binary files mode
if ( fs == NULL )
{
puts ( "Cannot open source file" ) ;
exit( 1) ;
}
ft = fopen ( "newpr1.exe", "wb" ) ;
if ( ft == NULL )
{
puts ( "Cannot open target file" ) ;
fclose ( fs ) ;
exit( 1) ;
}
while ( 1 )
{
ch = fgetc ( fs ) ;
if ( ch == EOF )
break ;
else
fputc ( ch, ft ) ;
}
fclose ( fs ) ;
fclose ( ft ) ;
}
#include <stdio.h>
#include<conio.h>
void main( )
{
FILE *fp ;
char another = 'Y' ;
struct emp
{
char name[40] ;
int age ;
float bs ;
};
struct emp e ;
fp = fopen ( "EMP.DAT", "wb" ) ;
if ( fp == NULL )
{
puts ( "Cannot open file" ) ;
exit(1 ) ;
}
while ( another == 'Y' )
{
printf ( "\nEnter name, age and basic salary: " ) ;
scanf ( "%s %d %f", e.name, &e.age, &e.bs ) ;
fwrite ( &e, sizeof ( e ), 1, fp ) ;
printf ( "Add another record (Y/N) " ) ;
fflush ( stdin ) ;
another = getch( ) ;
}
fclose ( fp ) ;
}
Explanation: