-->

Friday, 7 April 2017

FILE HANDLING CONCEPT

WRITE MODE:

e  AL            ALGORITHM:
·         Create a pointer variable for the type structure FILE
·         Use fopen() function to open the file in write mode ‘w’
·         If the function returns NULL, print an error message
·         Else use gets function to get a stream of characters from the user
·         Use fputs() function which outputs a string to the file pointer variable
·         Close the file pointer variable using fclose( ) function
·         After running the program open the file in the corresponding bin folder to view the contents entered by the user during run time.

PROGRAM
#include <stdio.h>
#include <string.h>
void main()
FILE *fileptr;
//pointer variable for the type FILE structure 
char ch[100]; 
clrscr(); 
fileptr=fopen("WriteDemo.txt","w"); 
//fopen function opens a stream for the file mentioned 
if(fileptr==NULL) 
{  
puts("Cant open file");   //puts outputs a string and appends a new line character  
exit(1); 
printf("\nEnter few lines:"); 
while(strlen(gets(ch))>0) 
{  
fputs(ch,fileptr);//outputs a string to a stream  
fputs("\n",fileptr); 
fclose(fileptr); 
getch();

OUTPUT  
Enter few lines: This is a demo to open a file in write mode  
--------------------------------------------------------------------------------------------------------------------------

APPEND MODE:

ALGORITHM:
·         Create a pointer variable for the type structure FILE
·         Use fopen() function to open the file in write mode ‘a’
·         If the function returns NULL, print an error message
·         Else use gets function to get a stream of characters from the user
·         Use fputs() function which outputs a string to the file pointer variable
·         Close the file pointer variable using fclose( ) function
·         After entering, go to the folder C:\TC\BIN to locate WriteDemo.txt file, that contains the same data entered during run time along with the existing data. The content of the text file would be This is a demo to open a file in write mode This is a demo to open a file in append mode

PROGRAM

#include <stdio.h>
#include <string.h>
void main()
FILE *fileptr; 
char ch[100]; 
clrscr(); 
fileptr=fopen("WriteDemo.txt","a");
//fopen function opens a stream for the file mentioned 
if(fileptr==NULL) 
{  
puts("Cant open file");  
//puts outputs a string and appends a new line character  
exit(1); 
printf("\nEnter few lines:"); 
while(strlen(gets(ch))>0) 
{  
fputs(ch,fileptr);//outputs a string to a stream  
fputs("\n",fileptr); 
fclose(fileptr); 
getch();
}

OUTPUT  

Enter few lines: This is a demo to open a file in append mode 
----------------------------------------------------------------------------------------------------------------

READING CONCEPT:

ALGORITHM:
·         Create a pointer variable for the type structure FILE
·         Use fopen() function to open the file in write mode ‘r’
·         If the function returns NULL, print an error message
·         Loop a fgetc function to get a character from the stream
·         Print the character until it reaches End Of File
·         Close the file pointer variable using fclose( ) function

PROGRAM
#include <stdio.h>
#include <conio.h>
void main()
FILE *fileptr; 
char ch; 
clrscr(); 
fileptr=fopen("WriteDemo.txt","r"); 
if(fileptr==NULL) 
{  
puts("Cant find file");  
exit(1); 
while(1) 
{  
ch=fgetc(fileptr);//gets a character from the stream  
printf("%c",ch); 
 if(ch==EOF)  
break; 
fclose(fileptr);
getch();
}

OUTPUT:
This is a demo to open a file in write mode
This is a demo to open a file in append mode 
-------------------------------------------------------------------------------------------------------------------------

REMOVING CONTENT:

ALGORITHM:
·         Create two pointer variables for the type structure FILE
·         Get the name of the existing file to be renamed from the user and the new name too using gets()
·         Pass them as input arguments to rename () function and if its successful the file is renamed with new name.
·         Else the function fails, print an error message
·         Using the first file pointer variable try to open the file with the old name in read mode with fopen()
·         The function returns NULL as the old file name doesn’t exists, so an error message is generated
·         Close the file pointer variable using fclose( ) function
·         Pass the new file name as input argument to the remove() function which removes the file from the directory
·         Using the second file pointer variable try to open the file with the new name in read mode with fopen()
·         The function returns NULL as the renamed file doesn’t exists, so an error message is generated
·         Close the file pointer variable using fclose( ) function

PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
char oldname[30],newname[30]; 
FILE *fptr,*fptr1; 
clrscr(); 
printf("Enter the file name to be renamed:"); 
gets(oldname); 
printf("\nEnter the new name:"); 
gets(newname);
 if(rename(oldname,newname)==0)  
printf("\nFile %s renamed to %s",oldname,newname); 
else   
perror("\nError"); 
fptr=fopen(oldname,"r"); 
 if(fptr==NULL) 
{    
printf("\nFile %s not found",oldname); 
fclose(fptr);  
remove(newname); 
fptr1=fopen(newname,"r"); 
if(fptr1==NULL)    
printf("\nFile %s not found",newname);
fclose(fptr1); 
getch();




OUTPUT

Enter the file name to be renamed: oldname.txt
Enter the new name: newname.txt
File oldname.txt renamed to newname.txt
File oldname.txt not found

File newname.txt not found 

-------------------------------------------------------------------------------------------------------------------------

COPYING CONTENTS:

ALGORITHM:
·         Create two pointer variables for the type structure FILE
·         Open the source file in read mode and target file in write mode using fopen function
·         Assign them to respective pointer variables
·         Read the source file data character by character using fgetc() function in a loop until End Of File is reached
·         Once the character is read use fputc function by passing the character and target file pointer variable as input arguments as the data is written in the target file by character
·         Close the file pointer variables using fclose( ) function
·         Check both the files located in the bin folder to see the same contents

PROGRAM

#include<stdio.h>
#include<conio.h>
void main()
{
   FILE *fileptr1, *fileptr2;
   char ch;
   clrscr();
   fileptr1=fopen("Message.txt","r");
   //open the source file in read mode
   fileptr2=fopen("MessageC.txt","w");
   //open the destination file in write mode
   //read the source file data character by character
   if(fileptr1==NULL)
   {
    puts("File not found");
    exit(1);
   }
   while(1)
   {
      ch=fgetc(fileptr1);
      //read a character from a stream in the fileptr1
      if(ch==EOF)
      break;
      else
        putc(ch,fileptr2);
       //putc outputs a character to a stream in fileptr2
   }
   printf("File copied successfully..");
   fclose(fileptr1);
   fclose(fileptr2);
   getch();
}

OUTPUT

File copied successfully..

-------------------------------------------------------------------------------------------------------------------------

ACCESSING DATA IN RANDOM MODE:

ALGORITHM:
·         Create a pointer variable for the type structure FILE
·         Open the file in write mode using fopen function
·         Write the data to the file using fwrite() function by passing pointer to any object, length of the data, number of data to be added and the file pointer variable as input arguments
·         Read the file data character by character using fseek() function by passing the file pointer variable, a numeric value which starts to read the data from that position and SEEK_SET which is 0 begins to read the data by assigning that position as index 0
·         Use fread() function which fread reads a specified number of equal-sized data items from an input stream into a block.
·         Close the file pointer variables using fclose( ) function
·         Check both the file located in the bin folder to see the contents
·         In the output we will see a part of the message which starts from the numeric value passed in fseek()

PROGRAM

//File manipulation using fread, fwrite, fseek
#include <stdio.h>
#include <conio.h>
void main()
{
   FILE *fptr;
   char msg[] = "this is a test";
   char buf[20];
   clrscr();
   if((fptr=fopen("DUMMY.TXT","w+"))==NULL)
   {
      puts("Cannot open output file.\n");
   }
   fwrite(msg,strlen(msg)+1,1,fptr);
   fseek(fptr,7,SEEK_SET);
   fread(buf,strlen(msg)+1,1,fptr);
   printf("%s\n",buf);
   fclose(fptr);
   getch();
}

OUTPUT

 a test 

  

keep calm and say bujuku bujuku.

0 comments:

Post a Comment

Start Work With Me

Contact Us
KUTTY SELVA
+91 7708139984
Madurai,Tamilnadu