Anda di halaman 1dari 16

// 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(); }

// 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(); }

// 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(); }

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); }

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

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; }

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_readin g); 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; } }

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

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 # ifdef # indef # else

# elif # include # define # undef

# 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.

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.

Conditional Compilation Directives

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

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; }

hai 6 16 Compi~1.c | Feb 16 2008 | Line Number : 29 hai new

#include <stdio.h> void main() {

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 :

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.

EXERCISES
1. Write a program to reverse the contents of a text file?

2.
3. 4.

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?

5.
6.

Explain the use of fseek() function in files?


Explain the concept of I/O in files with the help of fscanf() and fprintf() functions?

Anda mungkin juga menyukai