Sunday 4 July 2010

Write a file program explain how the data stored on the disk is read

//Before executing this program, you must create file emp.rec
 #include <stdio.h>
 #include <stdlib.h>
 #include <conio.h>

 void main()
 {
 FILE *fptr;
 char filename[15];
 char ch;
 clrscr();

 printf("Enter the filename to be opened\n");
 gets(filename);

 fptr = fopen (filename, "r"); /*open for reading*/

 if (fptr == NULL)
 {
 printf("Cannot open file\n");
 exit(0);
 }

 ch = fgetc(fptr);

 while (ch != EOF)
 {
 printf ("%c", ch);
 ch = fgetc(fptr);
 }

 fclose(fptr);
 getch();
 } /* End of main () */


Output

 /*----------------------------------------------
 Output
 Enter the filename to be opened
 emp.rec
 Name = Rama
 Age = 25
 Salary = 25000.00
 ------------------------------------------------*/
You may like the following posts:
Files
Data Structures

No comments:

Post a Comment