Sunday, September 22, 2013

File Management in C

"FILE MANAGEMENT---CHAPTER NO. 7"

Question No. 3
#include<stdio.h>
#include<conio.h>
void main(void)
{
                clrscr();
                FILE *ptr;
                char ch;
                int lb=0,rb=0;
                if((ptr=fopen("ASSIGN3.cpp","r"))==NULL)
                {
                printf("Cannot Open the File!\n");
                }
                else
                {
                ptr=fopen("ASSIGN3.cpp","r");
                                while((ch=getc(ptr))!=EOF)
                                {
                                                if(ch=='{')
                                                {
                                                lb++;
                                                }
                                                if(ch=='}')
                                                {
                                                rb++;
                                                }
                                }
                                fclose(ptr);
                                printf("Left Braces = %d and Right Braces = %d\n",lb,rb);
                                if(lb==rb)
                                {
                                printf("Left Braces and Right Braces are Equal");
                                }
                                else
                                {
                                printf("Left Braces and Right Braces are Not Equal");
                                }
                }
                getch();
}
Question No. 4
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main(void)
{
                clrscr();
                FILE *ptr;
                char name[15];
                int eng,urdu,math,isl,pst,computer;
                ptr=fopen("Result.txt","w");
                for(int i=0;i<5;i++)
                {
                clrscr();
                printf("Enter name of Student = ");
                scanf("%s",&name);
                printf("Enter marks in English = ");
                scanf("%d",&eng);
                printf("Enter marks in Urdu = ");
                scanf("%d",&urdu);
                printf("Enter marks in Math = ");
                scanf("%d",&math);
                printf("Enter marks in Islamiat = ");
                scanf("%d",&isl);
                printf("Enter marks in Pak-Studies = ");
                scanf("%d",&pst);
                printf("Enter marks in Computer = ");
                scanf("%d",&computer);
                fprintf(ptr,"%s %d %d %d %d %d %d",name,eng,urdu,math,isl,pst,computer);
                fprintf(ptr,"\n");
                }
                fclose(ptr);
                clrscr();
                printf("File Successfully Created!");
                getch();
}
Question No. 5
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main(void)
{
                clrscr();
                FILE *ptr;
                char name[15];
                int eng,urdu,math,isl,pst,computer,total,avg;
                ptr=fopen("Result.txt","r");
                printf("Name\tEng\tUrdu\tMath\tIsl\tP.St\tComp\tTotal\tAverage\n");
                while(fscanf(ptr,"%s %d %d %d %d %d %d",&name,&eng,&urdu,&math,&isl,&pst,&computer)!=EOF)
                {
                total=avg=0;
                total=eng+urdu+math+isl+pst+computer;
                avg=total/6;
                printf("%s\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\n",name,eng,urdu,math,isl,pst,computer,total,avg);
                }
                fclose(ptr);
                getch();

}

No comments:

Post a Comment