html5-img
1 / 16

Sequential & Random File Creation

Sequential &amp; Random File Creation. // Program uses fgets() to get a String from the File #include&lt;stdio.h&gt; #include&lt;conio.h&gt; void main() { FILE *fp; char s[80],n[25]; int c; clrscr(); printf(&quot;<br> Enter Your Filename :&quot;); gets(n); fp=fopen(n,&quot;r&quot;); if(fp==NULL) {

ahmadn
Download Presentation

Sequential &amp; Random File Creation

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Sequential & Random File Creation SRM-MCA

  2. // Program uses fgets() to get a String from the File #include<stdio.h> #include<conio.h> void main() { FILE *fp; char s[80],n[25]; int c; clrscr(); printf("\n Enter Your Filename :"); gets(n); fp=fopen(n,"r"); if(fp==NULL) { printf("\n Cannot open a File"); exit(0); } c=0; printf("\n The Contents of your file is\n"); while(fgets(s,80,fp)!=NULL) { printf("%s",s); c++; } printf("Total Lines :%d\n\n",c); fclose(fp); getch(); } // Program uses fputs() to send a String to a File #include<stdio.h> #include<conio.h> void main() { FILE *fp; char s[80]; clrscr(); fp=fopen("t1.txt","w"); if(fp==NULL) { printf("\n Cannot open a File"); exit(1); } printf("\n Enter Your Text\n"); while(strcmp(gets(s),"end")!=0) { fputs(s,fp); } puts("Data Can Be Updated Sucessfully"); getch(); }

  3. // Program uses fgetc() to get a character from the File #include<stdio.h> void main() { FILE *fp; char s[80]; int i,ch; fp=fopen("first.dat","r"); if(fp==NULL) { printf("\n Unable to open the File"); exit(0); } ch=fgetc(fp); for(i=0;i<80 && ch!=EOF;i++) { s[i]=ch; ch=fgetc(fp); } s[i]='\0'; printf("\n %s",s); fclose(fp); } // Program uses fputc() to send a character to an ASCII File #include<stdio.h> #include<conio.h> void main() { FILE *fptr; char strptr[]="This Is a Test \0"; char *p; clrscr(); fptr=fopen("first.dat","w"); if(fptr==NULL) { printf("\n Unable to open the File"); exit(); } for(p=strptr; *p != '\0';p++) { fputc(*p,fptr); } getch(); }

  4. // file creation : writing data to a file #include<stdio.h> #include<conio.h> FILE *fp; void main() { char name[20]; int no; int sal; fp=fopen("emp.dat","w"); if(fp != NULL) { printf("\nEnter the number:"); scanf("%d",&no); printf("\nEnter the name:"); scanf("%s",&name); printf("\nEnter the salary:"); scanf("%d",&sal); fprintf(fp,"The no is %d\n",no); fprintf(fp,"The name is %s\n",name); fprintf(fp,"The salary is %d\n",sal); printf("\n Suceesfully Created"); } else { printf("\n File Cannot Opened"); exit(0); } fclose(fp); } Read the contents from the file #include<stdio.h> #include<conio.h> FILE *fp; void main() { char c; clrscr(); if((fp=fopen("emp.dat","r"))!=NULL) { while((c=getc(fp))!=EOF) putc(c,stdout); } else { printf("Error in opening a file"); } fclose(fp); getch(); }

  5. Append the Data to an Existing File #include<stdio.h> FILE *fp; void main() { char name[20]; long int no; long int sal; if((fp=fopen (“emp.dat","a"))!=NULL) { printf("\n enter the number:"); scanf("%d",&no); printf("\n enter the name:"); scanf("%s",name); printf("\n enter the salary:"); scanf("%d",&sal); fprintf(fp,"the no is %d\n", no); fprintf(fp, "the name is %s\n",name); fprintf(fp, "the salary is %d\n",sal); } fclose (fp); }

  6. RANDOM FILE CREATION Write a “C” Program to add, View ,Modify and Delete Records in Electricity Board Bill Preparation using Random File printf("\n 1. Add Record "); printf("\n 2.View Record"); printf("\n 3.Delete a Record"); printf("\n 4.Modify a record"); printf("\n 5.Terminate the Program"); CHOICE : printf("\n Enter your choice "); scanf("%d",&ch); if((ch<1) && (ch>5)) { goto CHOICE; } switch(ch) { case 1:add_record(); break; case 2:view_record(); break; case 3:del_record(); break; case 4:modify_record(); break; case 5:exit(0); } #include<stdio.h> #include<stdlib.h> #include<conio.h> #include<dos.h> void add_record(); void view_record(); void modify_record(); void del_record(); struct electric { long int scno; char pname[30]; char area[20]; float cur_reading; float pre_reading; }; void main() { int ch,i; MENU: clrscr(); goto MENU; } void add_record() { struct electric r; FILE *eb; char flag; eb=fopen("eb.dat","ab+"); clrscr(); do { printf("\n Enter Details"); scanf("%ld %s %s %f %f",&r.scno,r.pname,r.area,&r.pre_reading,&r.cur_reading); fwrite(&r,sizeof(r),1,eb); fflush(stdin); printf("\n Add another record [y/n];"); flag=getchar(); }while(toupper(flag)=='Y'); fclose(eb); }

  7. void view_record() { struct electric r; FILE *eb; char flag; eb=fopen("eb.dat","rb+"); clrscr(); while((fread(&r,sizeof(r),1,eb)!=NULL)) { printf("\n The File datas Are \n\n"); printf("%ld %s %s %f %f",r.scno,r.pname,r.area,r.pre_reading,r.cur_reading); } } void del_record() { struct electric r; FILE *eb,*temp; long int no; char ch; temp=fopen("temp.dat","wb+"); eb=fopen("eb.dat","rb+"); clrscr(); printf("\n Enter the Number to Delete :"); scanf("%ld",&no); while((fread(&r,sizeof(r),1,eb)!=NULL)) { if(r.scno==no) { clrscr(); printf("\n The File datas Are \n\n"); printf("%ld %s %s %f %f",r.scno,r.pname,r.area,r.pre_reading,r.cur_reading); fflush(stdin); printf("\n Are U Sure to Delete this record[Y/N]:"); ch=getchar(); if(toupper(ch)=='Y') { fwrite(&r,sizeof(r),1,temp); continue; } } else { fwrite(&r,sizeof(r),1,temp); } } remove("eb.dat"); rename("temp.dat","eb.dat"); remove("temp.dat"); fclose(eb); fclose(temp); return; }

  8. void modify_record() { struct electric r; FILE *eb; long int no,pos; char ch; eb=fopen("eb.dat","rb+"); clrscr(); printf("\n Enter the Service Number to modify :"); scanf("%ld",&no); while(eb!=NULL) { pos=ftell(eb); fread(&r,sizeof(r),1,eb); if(r.scno==no) { clrscr(); printf("\n The File datas Are \n\n"); printf("%ld %s %s %f %f",r.scno,r.pname,r.area,r.pre_reading,r.cur_reading); fflush(stdin); printf("\n Are U Sure to Modify this record[Y/N]:"); ch=getchar(); if(toupper(ch)=='Y') { printf("\n Enter Details"); scanf("%ld %s %s %f %f",&r.scno,r.pname, r.area,&r.pre_reading,&r.cur_reading); fseek(eb,0,pos); fwrite(&r,sizeof(r),1,eb); puts("Record Updated Sucessfully"); getch(); } } else { puts("Service Number Not Found"); } fclose(eb); return; } }

  9. Points to be Remember • fopen() opens a stream for use and links a file with that stream • fclose() closes a stream that was opened by a call to fopen() • fputc()is used to write characters,fgetc() is used to read characters from an open file • fputs is used to write group of characters(string) fgets() is used to read group of characters from an open file • feof() is used to indicate end-of-file when the file is opened for binary operations • rewind() function resets the file position indicator to the beginning of the file • fprintf() is used to write any type of informations (any datatype) to an file,fscanf() is used to read any type of informations from an open file. • The ftell() used to indicate the current position of file pointer • The fseek() is used to set the position of filepointer which we can desired

  10. Preprocessor Directives • Preprocessor directives are instructions given to the compiler. • They begin with a # sign and can be placed anywhere in the program but usually they are placed in the beginning of a program. • Some of the preprocessor directives # if # elif # ifdef #include # indef # define # else # undef

  11. # define • The # define directive contains two parts : an identifier and a string that is to be substituted for the identifier, each time the identifier is encountered in the source file. # define identifier string • The identifier and string are separated by a space. • The identifier can be referred to as the macro name and the • string as the macro substitution.

  12. # if ... # endif # if ... # else ... # endif # if ... # elif ... # endif Conditional Compilation Directives # if, # else, # elif, # endif Syntax :

  13. printf("hai"); N; printf("%d",mul(2,3)); N; printf("%d",mysqrt(4)); N; printf("%s | %s | Line Number : %d",__FILE__,__DATE__,__LINE__); NF; NNN; } Example for Compiler Directives #define N putchar('\n') #define mul(x,y) ((x)*(y)) #define mysqrt(x) ((x) * (x)) #ifdef N #define FORMAT N,N #endif #ifdef FORMAT #undef FORMAT #define NF N,printf("hai"),N #endif #ifdef NF #ifndef FORMAT #define NNN N,printf("new"),N #endif #endif #include <stdio.h> void main() { Output hai 6 16 Compi~1.c | Feb 16 2008 | Line Number : 29 hai new

  14. The const Keyword • It can be used to achieve almost the same effect as #define when creating constants. • Variables of type const can be given an initial value, but cannot be changed through the program. • Example : const int var = 35;

  15. Session Summary • Storage of information to be read from or written on a auxillary memory device is stored in the form of file. • The data structure of a file is defined in stdio.h which creates a buffer area to store data in a file for reading as well as writing • Function fscanf() & fprintf() are used to perform I/O operations in file • Function fgetc() & fputc() are used to perform Character I/O operations in file • Function fgets() & fputs() are used to perform String I/O operations in file • Function fseek() used to index a file and can be used to increment or decrement the file pointer by any number of position in a file.

  16. EXERCISES Write a program to reverse the contents of a text file? Write a program to count the number of characters in a text file? Write a program to count the number of words in a file? Write a program to merge three text files one by one? Explain the use of fseek() function in files? Explain the concept of I/O in files with the help of fscanf() and fprintf() functions?

More Related