Files in C

Download as
 PPT
Presentation Description 

No description available

Views: 150
Like it  ( Likes) Dislike it  ( Dislikes)
Added: December 24, 2008 This Presentation is Public 
Presentation Category : Education All Rights Reserved
Tags Add Tags
Presentation Statistics
Views on authorSTREAM: 148 | Views from Embeds: 2
Others - 2 views
Presentation Transcript

Slide 1:File File: A file is a collection of bytes stored on a secondary storage device, which is generally a disk of some kind. The collection of bytes may be interpreted, for example, as characetrs, words, lines, paragraphs and pages from a textual document; fields and records belonging to a database; or pixels from a graphical image. The meaning attached to a particular file is determined entirely by the data structures and operations used by a program to process the file.


Slide 2:Opening a file: Before we can write to a file, we must open it. What this really means is that we must tell the system that we want to write to a file and what the filename is. We do this with the fopen function illustrated in the first line of the program. The file pointer, fp in our case, points to the file and two arguments are required in the parentheses, the filename first, followed by the file type.


Slide 3:Reading (r): The second parameter is the file attribute and can be any of three letters, r, w, or a, and must be lower case. When an r is used, the file is opened for reading, a w is used to indicate a file to be used for writing, and an a indicates that you desire to append additional data to the data already in an existing file. Most C compilers have other file attributes available; check your Reference Manual for details. Using the r indicates that the file is assumed to be a text file. Opening a file for reading requires that the file already exist. If it does not exist, the file pointer will be set to NULL and can be checked by the program.


Slide 4:Writing (w) When a file is opened for writing, it will be created if it does not already exist and it will be reset if it does, resulting in the deletion of any data already there. Using the w indicates that the file is assumed to be a text file.


Slide 5:Program: #include #include void main() { int rl; char name[20]; float m1,m2; char ch; FILE *fp; fp=fopen(“std.dat”,”w”); clrscr();


Slide 6:ph: printf(“enter the student name:”); scanf(“%s”,&name); printf(“enter the roll no:”); scanf(“%d”,&roll no); printf(“enter the mark1:”); scanf(“%d”,&mark1); printf(“enter the mark2:”); scanf(“%s”,&mark2); clrscr(); fprintf(fp,”%s/n”,name); fprintf(fp,”%d/n”,rl); fprintf(fp,”%d/n”,m1); fprintf(fp,”%d/n”,m2);


Slide 7:fflush(stdin); printf(“do u want to continue y/n”); scanf(“%c”,&ch); if(ch==‘y’||ch==‘Y’) goto ph; else fclose(fp); getch(); }