Monday, 18 June 2018

File Input/Output File Scan

By using File Input/Output , we gonna count:-
No. of characters
No. of blanks
No. of tabs
No. of lines

So, here the code for the above.

/*Pre Processors */
#include<stdio.h>
#include<conio.h>

int main()     /*Main function*/
{
    FILE *fp;
    char ch;
    clrscr();
    int nol=0, not=0, nob, noc=0;
    fp=fopen("EMPLOYEE.TXT","r");
    while(1)
    {
        ch=fgetc(fp);
        if(ch==EOF)
        break;
        noc++;
        if(ch==' ')
        nob++;
        if(ch=='\n')
        nol++;
        if(ch=='\t')
        not++;
    }
    fclose(fp);
    printf("\nNumber of characters:- %d",noc);
    printf("\nNumber of blanks:- %d",nob);
    printf("\nNumber of tabs:- %d",not);
    printf("\nNumber of lines:- %d",nol);
    getch();
    return 0;
}

Monday, 11 June 2018

Program to read an existing file using File Handling

File Handling is concept that is used to read or write a file in your hard disk using some lines of code.
Here we gonna do the same using C langugae

/*Pre Processor used throughout the program*/
#include<stdio.h>
#include<conio.h>

int main()
{
    FILE *fp;
    char ch;
    clrscr();
    fp=fopen("Employee.txt","r");
    while(1)
    {
        ch=fgetc(fp);
        if(ch==EOF)
        break;
        printf("%c",ch);
    }
    printf("\n");
    fclose(fp);
    getch();
    return 0;

}


Sunday, 10 June 2018

Program to Write a Text File using FIle Handling

File Input/Output is used to store or fetch desirable date using C language. It is basically come in use when we want to store data and make sure it's privacy.

So, Here a Simple program for the creation of a Text format file using C language that comes under File Input/Output.

/*Pre Processors used throughout the program*/
 #include<stdio.h>
 #include<conio.h>

 int main()
 {
    FILE *fp;                     /*Creation of a file type pointer*/
    char ch='Y';                
    struct emp                    /*Structure of a user defined data type i.e emp*/
    {
        char name[40];
        int age;
        float bs;
    };
    clrscr();
    struct emp e;
    fp=fopen("Employee.txt","w");             /*create a txt format file using fopen*/
    if(fp==NULL)
    {
        puts("\nCannot open file");
    }
    while(ch=='Y')
    {
        printf("\nEnter your data");
        printf("\nEnter Name:- ");
        scanf("%s",&e.name);
        printf("\nEnter Age:- ");
        scanf("%d",&e.age);
        printf("\nEnter Salary:- ");
        scanf("%f",&e.bs);

        fprintf(fp,"Name:- " );
        fprintf(fp,"%s \n",e.name);
        fprintf(fp,"Age:- ");
        fprintf(fp,"%d \n",e.age);
        fprintf(fp,"Salary:- ");
        fprintf(fp,"%f \n\n",e.bs);

        printf("\nDo you want to add another data(Y/N):- ");
        fflush(stdin);
        ch=getche();
    }
    fclose(fp);                             /*closing of file using fclose*/
    return 0;
 }



And the file that store hold the data of your program is below:-