C Program Day-14

  • Upload
    eshamu

  • View
    217

  • Download
    0

Embed Size (px)

Citation preview

  • 7/31/2019 C Program Day-14

    1/16

  • 7/31/2019 C Program Day-14

    2/16

    // Program uses fgets() to get a String from theFile#include#includevoid 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 aFile#include#includevoid 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();}

  • 7/31/2019 C Program Day-14

    3/16

    // Program uses fgetc() to get acharacter from the File#includevoid 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

  • 7/31/2019 C Program Day-14

    4/16

    // file creation : writing data to a file#include#includeFILE *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#includeFILE *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();}

  • 7/31/2019 C Program Day-14

    5/16

    Append the Data to an Existing File

    #includeFILE *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); }

  • 7/31/2019 C Program Day-14

    6/16

    #include#include#include#includevoid add_record();void view_record();voidmodify_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();

    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 theProgram");CHOICE :printf("\n Enter your choice ");scanf("%d",&ch);if((ch5)){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);}

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

    }

    Write a C Program to add, View ,Modify and Delete Records in Electricity Board Bill Preparation using

    Random File

  • 7/31/2019 C Program Day-14

    7/16

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

  • 7/31/2019 C Program Day-14

    8/16

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

  • 7/31/2019 C Program Day-14

    9/16

    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

  • 7/31/2019 C Program Day-14

    10/16

    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

  • 7/31/2019 C Program Day-14

    11/16

    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.

  • 7/31/2019 C Program Day-14

    12/16

  • 7/31/2019 C Program Day-14

    13/16

    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

    void main()

    {

    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

  • 7/31/2019 C Program Day-14

    14/16

    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, butcannot be changed through the program.

    Example :

  • 7/31/2019 C Program Day-14

    15/16

    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.

  • 7/31/2019 C Program Day-14

    16/16

    EXERCISES

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

    2. Write a program to count the number of characters in a text file?

    3. Write a program to count the number of words in a file?

    4. Write a program to merge three text files one by one?

    5. Explain the use of fseek() function in files?

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